简体   繁体   English

Ajax请求后页面刷新

[英]Page refreshes after ajax request

Alright, I have a simple form comprising of only a text field . 好吧,我有一个仅包含text field的简单表单。 Data written in the text field is stored in DB when we hit submit (Stored via ajax). 当我们点击submit(通过ajax存储)时,在文本字段中写入的数据将存储在DB中。 The ajax works fine and data is submitted, however, the page get's refreshed automatically and the URL contains the content of the input field. Ajax可以正常工作并提交数据,但是,页面会自动刷新,并且URL包含输入字段的内容。

My Form :- 我的表格:

<form class="form-horizontal">
                <fieldset>

                <!-- Text input-->
                <div class="form-group">
                  <label class="col-md-4 control-label" for="message"></label>  
                  <div class="col-md-5">
                  <input id="message" name="message" type="text" placeholder="message" class="form-control input-md" required="">

                  </div>
                </div>

                <!-- Button -->
                <div class="form-group">
                  <label class="col-md-4 control-label" for="submit_message"></label>
                  <div class="col-md-4">
                    <button id="submit_message" name="submit_message" class="btn btn-success">Enter</button>
                  </div>
                </div>

                </fieldset>
                </form>

Ajax :- 阿贾克斯:-

$("#submit_message").click(function() {
    var message = $("#message").val();
    $.ajax({
      type: "POST",
      url: "ajax_getter.php?requestid=2",
      data: { message: message, c: c },
      dataType: "html"
    }).done(function( msg ) {
      //load_content();
      alert(msg);
});
});

PHP :- PHP的:-

//...
if($chat->insert("chat_threads", $arr))
    {
        echo 1;
    }
    else
    {
        echo 0;
    }

After the result is show in the popup , the page refresh and the URL becomes something like :- chat.php?message=454545&submit_message= Why is the page being refreshed ? 结果显示在popup ,页面将刷新,URL变为: -chat.php?message = 454545&submit_message =为什么要刷新页面?

Seems that your form is being submitted. 似乎您的表单正在提交。 Try preventing the default event (ie submission): 尝试阻止默认事件(即提交):

$("#submit_message").click(function(e) {
    e.preventDefault();    // This prevents form from being sumbitted
    // the rest of your code
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM