[英]AJAX doesn't send data, I can't use POST Result
EDIT :编辑 :
I would like to have a button value in another page.我想在另一个页面中有一个按钮值。 I've think to use Ajax, but I dn"t know if it's the best way..我想使用 Ajax,但我不知道这是否是最好的方法..
Here is my Form:这是我的表格:
<form id="form_prj">
<button class="btn_prj" type="button" name="var_prj" value=60>Afficher</button>
</form>
Here is my Ajax: projet.php这是我的Ajax:projet.php
$(document).ready(function() {
$(document).on('click', '.btn_prj', function() {
var sendData = {
lid: this.value
};
$.ajax({
type: 'POST',
url: '../pit_app/carte.php',
cache: false,
data: {
lid: sendData,
},
success: function(data) {
console.log(data);
window.location = './carte.php';
},
error: function() {
alert("Error Occured");
}
});
});
Here get_data.php这里get_data.php
<?php
include('fonctions.php');
$connect = connect();
if(isset($_POST["lid"])) {
$btn_value = $_POST['lid'];
}else{
echo ("Error");
}
$sql1 = "SELECT row_to_json(fc)FROM (
SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
FROM (
SELECT 'Feature' As type, ST_AsGeoJSON(ST_transform(ST_SetSRID(geom, 2154),
4326))::json As geometry, row_to_json(n) As properties
FROM chambre As n,projet WHERE ST_Contains(zone_etude,geom) and id_projet=" . $btn_value . ") As f) As fc;";
$result = pg_query($connect, $sql1);
$rs = pg_fetch_array($result);
$legeojson = $rs[0];
?>
EDIT2:编辑2:
Ok, I added my query to the isset and now I have another problem.好的,我将我的查询添加到了isset,现在我有另一个问题。 In my third file, carte.php where I included get_data.php在我的第三个文件 carte.php 中,我包含了 get_data.php
I have var geojson = </ br>
But I need我有var geojson = </ br>
但我需要
var geojson = <?php echo $legeojson;?>;
Notice: Undefined variable: legeojson in E:\\wamp\\www\\stage_axians\\pit_app\\carte.php on line 108注意:未定义变量:108 行 E:\\wamp\\www\\stage_axians\\pit_app\\carte.php 中的 legeojson
I've made some small clean up of your code.我对你的代码做了一些小的清理。 Your click action was a bit wrong, to get the button instance by its name you can usee $('[name="var_prj"]')
which mean that $(this)
will return exactly this button in the body of the click
event您的点击操作有点错误,要通过其名称获取按钮实例,您可以使用$('[name="var_prj"]')
这意味着$(this)
将在click
事件的正文中准确返回此按钮
Next, to get the value from the button value
attribute you can use $(this).attr('value')
if you want to get value from input
tags you can use simple $(this).val()
.接下来,要从按钮value
属性中获取值,您可以使用$(this).attr('value')
如果您想从input
标签中获取值,您可以使用简单的$(this).val()
。
And the last part is a data
parameter of your AJAX
call.最后一部分是AJAX
调用的data
参数。 If you already have an object with key and value, just put to the data
param of the $.ajax
如果您已经有一个带有键和值的对象,只需放入$.ajax
的data
参数
<form id="form_prj">
<button type="button" name="var_prj" value=60>Afficher</button>
</form>
$(document).ready(function () {
$('[name="var_prj"]').click(function () {
var sendData = {
lid: $(this).attr('value'),
};
$.ajax({
type: 'POST',
url: '../pit_app/carte.php',
cache: false,
data: sendData,
success: function (data) {
window.location = './carte.php';
},
error: function () {
alert('Error Occured');
},
});
});
});
Here is the $_POST
array of your carte.php
after sending AJAX request.这是发送 AJAX 请求后carte.php
的$_POST
数组。
Array
(
[lid] => 60
)
Hope my answer would help to you希望我的回答对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.