在我按下“提交”按钮后,变量$pos
没有值。
<?php
$pos = $_GET['pos'];
echo "<br><br>pos = ".$pos;
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=us-ascii">
<title></title>
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'></script>
<script type='text/javascript'>
$(window).load(function(){
var position = 128;
$('#submit').click(function(){
$.ajax({
type:"GET",
url: "ajax_test.php",
data: { pos : position },
success: function(){
//do stuff after the AJAX calls successfully completes
}
});
});
});
</script>
</head>
<body>
<br>
<button id="submit">Submit</button>
</body>
</html>
FF中的Web控制台显示:
[12:41:55.027] GET http://somehost.com/ajax_test.php?pos=128 [HTTP/1.1 200 OK 187ms]
答案 0 :(得分:3)
<?php
if($_SERVER['REQUEST_METHOD']=='GET' && isset($_GET['ajax'])){
$pos = $_GET['pos'];
echo $pos; exit;
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=us-ascii">
<title></title>
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'></script>
<script type='text/javascript'>
$(window).load(function(){
var position = 128;
$('#submit').click(function(){
$.ajax({
type:"GET",
url: "test.php",
data: { pos : position , ajax:1},
success: function(response){
document.getElementById('response').innerHTML ='pos='+response
//do stuff after the AJAX calls successfully completes
}
});
});
});
</script>
</head>
<body>
<br>
<div id="response">pos=</div>
<button id="submit">Submit</button>
</body>
</html>
答案 1 :(得分:1)
您的代码是正确的,您只是没有使用Ajax GET请求的结果。
在您的成功函数中尝试此操作:
success: function(data) {
alert(data);
}
答案 2 :(得分:1)
我没有在您的代码中看到任何错误,除了您从JavaScript代码调用PHP脚本,并且不对输出执行任何操作。 echo $ pos输出不会在任何地方使用。将其添加到您的脚本中:
success: function(result){
$('#YourResultDiv').html(result);
}
答案 3 :(得分:0)
您无法从AJAX调用中检索变量。
Ajax是异步的,你不能修改已经计算过的php代码。
但您可以使用jQuery插入数据,例如<{1}}