[英]ajax not able to pass variable to php
I have a slider which uses javascript. 我有一个使用JavaScript的滑块。 I am trying to update the display of my web page based on the slider values. 我正在尝试根据滑块值更新网页的显示。 I tried to use ajax function to send the data to another PHP page to update the display. 我试图使用ajax函数将数据发送到另一个PHP页面以更新显示。 But I am not getting anything in my page. 但是我的页面没有任何内容。 Here is my code so far. 到目前为止,这是我的代码。
<?php
$i = 1;
while (++$i <= $_SESSION['totalcolumns']) {
$range = $_SESSION["min-column-$i"] . ',' . $_SESSION["max-column-$i"];?>
<br><?php echo "Keyword" ?>
<?php echo $i -1 ?>
<br><input type="text" data-slider="true" data-slider-range="<?php echo $range ?>" data-slider-step="1">
<?php } ?>
<button type="button" onclick="loadXMLDoc()">Update</button>
<script>
$("[data-slider]")
.each(function () {
var range;
var input = $(this);
$("<span>").addClass("output")
.insertAfter(input);
range = input.data("slider-range").split(",");
$("<span>").addClass("range")
.html(range[0])
.insertBefore(input);
$("<span>").addClass("range")
.html(range[1])
.insertAfter(input);
})
.bind("slider:ready slider:changed", function (event, data) {
$(this).nextAll(".output:first")
.html(data.value.toFixed(2));
});
</script>
<script>
function loadXMLDoc()
{
alert "Am I coming here";
$.ajax({
type: "POST",
url: 'update.php',
data: { value : data.value },
success: function(data)
{
alert("success!");
}
});
}
</script>
I read in another post that javascript variables are available across functions and so I am trying to use the variable data.value inside my another javascript function loadXMLDoc() . 我在另一篇文章中读到,JavaScript变量可用于各个函数,因此我试图在另一个JavaScript函数loadXMLDoc()中使用变量data.value 。 But I do not see the value getting displayed in my update.php page. 但是我看不到该值显示在我的update.php页面中。 My update.php file is as below. 我的update.php文件如下。
<?php
if(isset($_POST['value']))
{
$uid = $_POST['value'];
echo "Am I getting printed";
echo $uid;
}
?>
Can someone please help me on this? 有人可以帮我吗?
In the loadXMLDoc function I don't see data defined anywhere. 在loadXMLDoc函数中,我看不到任何地方定义的数据。 I think that could be one of the problems. 我认为这可能是问题之一。 Also, when you're doing jquery ajax requests be sure to have a fail callback. 另外,在执行jquery ajax请求时,请确保具有失败的回调。 The fail callback will tell you if the request fails which can be very informative. 失败回调将告诉您请求是否失败,这可能会提供很多信息。
var jqxhr = $.ajax( "example.php" )
.done(function() {
alert( "success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "complete" );
});
To make the data variable accessible in the XMLLoadDoc function you could try putting it in the global scope (kind of a 'no-no', but its OK for a use case like this). 为了使数据变量在XMLLoadDoc函数中可访问,您可以尝试将其放在全局范围内(有点“不可以”,但是对于这样的用例来说可以)。 So, at the top, declare var masterData
, then when you have data in the .bind callback set masterData = data;
因此,在顶部声明var masterData
,然后在.bind回调中包含数据时,设置masterData = data;
and then in loadXMLDoc refer to masterData
然后在loadXMLDoc中引用masterData
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.