Ajax使用GET将变量传递给PHP

Ajax使用GET将变量传递给PHP,php,ajax,Php,Ajax,在我按下提交按钮之后,变量$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>

在我按下提交按钮之后,变量
$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>

您的代码是正确的,只是没有使用Ajax GET请求的结果

在成功功能中尝试以下方法:

success: function(data) {
   alert(data);
}

您的代码是正确的,只是没有使用Ajax GET请求的结果

在成功功能中尝试以下方法:

success: function(data) {
   alert(data);
}

我在代码中没有看到任何错误,除了从JavaScript代码调用PHP脚本,并且不对输出执行任何操作。echo$pos输出不在任何地方使用。将以下内容添加到脚本中:

success: function(result){
    $('#YourResultDiv').html(result);
}

我在代码中没有看到任何错误,除了从JavaScript代码调用PHP脚本,并且不对输出执行任何操作。echo$pos输出不在任何地方使用。将以下内容添加到脚本中:

success: function(result){
    $('#YourResultDiv').html(result);
}

无法从AJAX调用中检索变量

Ajax是异步的,您不能修改已经计算过的php代码


但是您可以使用jQuery插入数据,例如
appendTo()
,您无法从AJAX调用中检索变量

<?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>
Ajax是异步的,您不能修改已经计算过的php代码

但是您可以使用jQuery插入数据,例如
appendTo()


<?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>
$(窗口)。加载(函数(){ var位置=128; $(“#提交”)。单击(函数(){ $.ajax({ 键入:“获取”, url:“test.php”, 数据:{pos:position,ajax:1}, 成功:功能(响应){ document.getElementById('response').innerHTML='pos='+response //在AJAX调用成功完成后执行操作 } }); }); });
位置= 提交

$(窗口)。加载(函数(){
var位置=128;
$(“#提交”)。单击(函数(){
$.ajax({
键入:“获取”,
url:“test.php”,
数据:{pos:position,ajax:1},
成功:功能(响应){
document.getElementById('response').innerHTML='pos='+response
//在AJAX调用成功完成后执行操作
}
});
});
});

位置= 提交
#提交是提交类型的输入,对吗?它位于表单内部,对吗?$pos未定义。只有pos定义您必须了解HTTP协议,什么是请求和响应,以及如何使用它们-此后所有内容都将直接流动。#submit是submit类型的输入,对吗?它位于表单内部,对吗?$pos未定义。只有pos定义您必须了解HTTP协议,什么是请求和响应,以及如何使用它们-之后一切都将直接进行。为什么要在同一代码中混合
$('someID')
document.getElementById()
?为什么要在同一代码中混合
$('someID')
document.getElementById()