[英]Passing AJAX variable into .php page and load that page and echo variable
一个简单的问题:我只想将item_num
发送到insert_profile.php,然后加载insert_profile.php并在其中回item_num
。 没用! “ insert_profile.php”将加载,但未获取数据。
我的data:
有问题吗? 我也尝试过data: {item_num: item_num}
。 很抱歉,如果这是重复的,但是我已经尝试了所有示例,但都没有用。 我认为也可能会success:
我还查看了http://api.jquery.com/jQuery.ajax/ 。
Javascript / HTML
<script>
$('.prof_wl_btn').click(function() {
$(this).removeClass('prof_wl_btn');
$(this).addClass('prof_wl_btn_added');
var item_num = this.id;
alert('item id ' + item_num); //*this will alert correctly*
$.ajax({
type: "POST",
url:'insert_profile.php',
data: "item_num"+item_num,
success: location.href = "insert_profile.php" //*this will load**
});
});
</script>
PHP
<?php
$s_n = $_REQUEST['item_num'];
echo $s_n;
?>
您无法以这种方式回显变量,因为您对同一页面有两个不同的请求:
insert_profile.php
页面时,第二个值丢失。 如果您希望看到您的值被回显,则可以执行以下操作:
AJAX调用应如下所示:
$.ajax({
type: "POST",
url:'insert_profile.php',
data: {item_num : item_num},
success : function(data) {
alert(data);
}
});
然后,您的PHP文件:
<?php $s_n = $_POST['item_num'];
echo $s_n;
exit;
?>
$.ajax({
type: "POST",
url:'insert_profile.php',
data: "item_num = "+item_num,
success : function( data ) {
// location.href = "insert_profile.php" //*this will load**
alert(data);
}
});
在PHP代码中,您喜欢这样并检查
<?php
echo 'OK';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.