weixin_33736649 2018-10-04 12:16 采纳率: 0%
浏览 18

PHP响应逐渐消失

I am trying to get response trough ajax,my code (index.html) :

<button id="register">Register</button>
<p id="result">xxx</p>

<script>
    $("#register").click(function(){
        $.ajax({
            url:'registration.php',
            type: 'POST',
            success:function(response){
                $("#result").html(response)
            }
        })
    })
</script>

and php (registration.php):

<?php 
echo "yyy"
?>

I am working with xampp, I get response but it fades from page instantly. And again xxx is present inside p tag, does anyone know what is the reason why this happens. Thanks

  • 写回答

2条回答 默认 最新

  • weixin_33726313 2018-10-04 12:23
    关注

    It appears that when you click the button to get your response, it also refreshes the page in your browser. You can try the following to prevent this:

    <script>
    $("#register").click(function(evt) {
      evt.preventDefault()
    
      $.ajax({
        url:'registration.php',
        type: 'POST',
        success: function (response) {
          $("#result").html(response)
        }
      })
    })
    </script>
    

    This prevents your browser from doing what it normally does when you click a button.Any button inside <form>tags will automatically send a GET request within the current window, causing the page to refresh. The other alternative to preventDefault() is to use the attribute type="button" on your button and this will stop the button from being a type="submit" button.

    You can read more detailed information about the function I've used here:

    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-matlab)
  • ¥15 手机添加cocks5协议没网络
  • ¥30 实验 :UML2.0 行为建模
  • ¥15 设计一种优化调度算法并结合论文中的案例给出调度计划
  • ¥15 py文件打包exe报错TypeError:expected str,bytes or os.PathLike object,not_io.BytesIO
  • ¥15 Java编写学籍信息管理系统
  • ¥15 IPXE网刻客户机找不到服务端
  • ¥15 小贝360-4 配二个 华772S 设置WⅰFi5G 连接
  • ¥15 vs2022的QT报错,好像是缺少winextras
  • ¥15 怎么看 cst中一个面的功率分布图