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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?