斗士狗 2016-02-14 12:39 采纳率: 0%
浏览 48

Ajax和jQuery与PHP

I have this code which posts data using jQuery and ajax to itself.

<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; 
  ?>

Now when I try to run this code.I do get the ok status inside the Network Console of Chrome but this code doesn't echo the output.

I simply want to display the result that has been passed by the ajax method.

I am new to ajax and jquery so i don't know much about how they work exactly but is it possible? If yes, then how could i achieve that without actually refreshing the page?

  • 写回答

1条回答 默认 最新

  • weixin_33713707 2016-02-14 12:54
    关注

    Here is what I think you want, i.e. test.php is the source page AND the ajax page!

    <?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
        }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样