Ajax and jQuery with PHP

Ajax and jQuery with PHP

本文关键字:PHP with jQuery and Ajax      更新时间:2023-09-26

我有这段代码,它使用jQuery和ajax将数据发布到自身。

<input type="button" id="butt"  value="button11111111111111" >
<script>
$("#butt").on('click',function(e)
    {
        $.ajax(
            {
                type:'POST',
                url:'test.php',
                data:product_type:"cake"
            });
    });
</script>
<?php
if(isset($_POST['product_type']))
         $abc=$_POST['product_type'];
 if(isset($abc))
      echo $abc; 
  ?>

现在,当我尝试运行此代码时。我确实在 Chrome 的网络控制台中获得了正常状态,但此代码不会回显输出。

我只想显示 ajax 方法传递的结果。

我是 ajax 和 jquery 的新手,所以我不太了解它们的工作原理,但这可能吗?如果是,那么如何在不实际刷新页面的情况下实现这一目标?

这是我认为你想要的,即测试.php是源页面 ajax 页面!

<?php
    if(isset($_POST['product_type'])) {
        Header("Content-type: text/plain; charset=utf-8");
        $abc=$_POST['product_type'];
        if(isset($abc)) {
            echo $abc;
        }
    }
    else {
?>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">   </script>
<input type="button" id="butt"  value="button11111111111111" >
<script>
    $("#butt").on('click',function(e) {
        $.ajax({
            type:'POST',
            url:'test.php',
            data: {
                product_type:"cake"
            },
            success: function(data) {
                alert('got ' + data);
            }
        });
    });
</script>
</html>
<?php
    }
?>