[英]Ajax not sending javascript variable to PHP
I've gone as far as I can with the below script, but I can't for the life of me work out why the page id is not being sent to the PHP script. 我已经尽可能地使用下面的脚本,但我不能为我的生活找出为什么页面ID没有被发送到PHP脚本。
Currently I'm grabbing the "index.php?id=12" with a PHP _GET command then inserting that into a Javascript variable. 目前我正在使用PHP _GET命令抓取“index.php?id = 12”,然后将其插入到Javascript变量中。 I set up a 2nd variable that includes that, as well as a string for latitude and longitude.
我设置了第二个变量,包括它,以及纬度和经度的字符串。 When I test that the string is created via an alert, the string looks exactly like it should.
当我测试通过警报创建字符串时,字符串看起来应该是它应该的样子。
Inside my PHP script, I only every get the longitude and latitude data, but never the id data. 在我的PHP脚本中,我只获取经度和纬度数据,但从不获取id数据。 Having read a number of other threads on here I believe the Ajax is expecting an object and that the string is unexpected.
在这里阅读了许多其他线程后,我相信Ajax期待一个对象,并且该字符串是意外的。 Unfortunately I do not know how to rectify this.
不幸的是我不知道如何纠正这个问题。
My Javascript is as follows: 我的Javascript如下:
function GeoSuccess(position) {
var id = <?php echo $id ?>;
var dataString = '?id='+id+'&action=geolocation&latitude='+position.coords.latitude+'&longitude='+position.coords.longitude;
$.ajax({
url: 'search.php',
type: 'GET',
data: dataString,
success: function (msg) {
$('div#search-results').html(msg);
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert('Error submitting request.');
}
});
}
use 采用
'id='+id+....
instead of 代替
'?id='+id+....
If you want an object rather than a string, this would work: 如果你想要一个对象而不是一个字符串,这将是有效的:
var data = {
id: id,
action: 'geolocation',
latitude: position.coords.latitude,
longitude: position.coords.longitude,
}
$.ajax({
url: 'search.php',
type: 'GET',
data: data,
// .... snip
I'm not sure but i don't think that '?' 我不确定,但我不认为'?' is expected in your dataString.
在您的dataString中是预期的。 Removing it could work too.
删除它也可以工作。
Try to assign an object to ajax data parameter 尝试将对象分配给ajax数据参数
data: {
id : <?php echo $id; ?>,
action : "",
latitude : position.coords.latitude,
longitude : position.coords.longitude
}
function GeoSuccess(position) {
var id = <?php echo $id ?>;
var dataObject = new Object();
dataObject.id=id;
dataObject.action=geolocation;
dataObject.altitude=position.coords.latitude;
dataObject.longitude=position.coords.longitude;
$.ajax({
url: 'search.php',
type: 'POST',
data: dataObject ,
success: function (msg) {
$('div#search-results').html(msg);
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert('Error submitting request.');
}
});
}
try this..che 试试这个......
You can add dataString to the url 您可以将dataString添加到url
url: 'search.php' + dataString,
or you can remove the leading '?' 或者你可以删除前导'?' from the dataString
来自dataString
var dataString = 'id='+id+'&action=geolocation&latitude='+position.coords.latitude+'&longitude='+position.coords.longitude;
This way you will get the id. 这样你就可以获得id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.