如何在另一个页面上回显来自ajax调用的Post

问题描述 投票:0回答:1

我是 jqeury ajax 新手,我尝试发布一些内容

testso.php
。我尝试在 testso.php 上回显
$_POST['test']
,但它没有显示任何内容。如果我打开开发人员工具并检查预览,我可以看到数据在那里。我是不是做错了什么?

测试.php

<script>
   var test = '123';
   
        $.ajax({
            type: "POST",
            url: "testso.php",
            data: { test: test },
            success: function(response) {
            console.log(response);
            }
        });

</script>

testso.php

<?php

if(isset($_POST['test'])){

    $img = $_POST['test']; 
?>

<div><?php  echo $img;  ?></div>

<?php  } ?>

preview on browser

我尝试使用

document.getElementById('ajaxreturn').innerHTML=test;
但这不是我想要的。也许有人可以建议我如何回应帖子。

php jquery ajax
1个回答
0
投票

使用PHP生成ajax使用的一些数据是完全有效且常见的。您的示例实际上可以工作,但结果不在页面上,而是在控制台上。

试试这个:

<script>
    var test = '123';
    $.ajax({
        type: "POST",
        url: "testso.php",
        data: { test: test },
        success: function(response) {
            document.getElementById('ajaxreturn').innerHTML = response;
        }
    });
</script>
<div id="ajaxreturn"></div>
<?php

if(isset($_POST['test'])){
    echo 'php ' . $_POST['test']; 
}else{
    echo "php not set."; 
}
© www.soinside.com 2019 - 2024. All rights reserved.