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:

    评论
  • hurriedly% 2018-10-04 12:34
    关注

    Just prevent page before submitting.

    <input type='submit' id='register'>
    <div id='result'></div>
    $("#register").click(function(e){
     e.preventDefault();
     $.ajax({
      url:'registration.php',
      type: 'GET',
      success:function(response){
       document.getElementById("result").innerHTML = response;
      }
     })
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 存储过程或函数中的结果集类型变量如何使用。
  • ¥80 关于海信电视聚好看安装应用的问题
  • ¥15 vue引入sdk后的回调问题
  • ¥15 求一个智能家居控制的代码
  • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
  • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
  • ¥20 求各位能用我能理解的话回答超级简单的一些问题
  • ¥15 yolov5双目识别输出坐标代码报错
  • ¥15 这个代码有什么语法错误
  • ¥15 给予STM32按键中断与串口通信