Home >Backend Development >PHP Tutorial >Why does the page refresh when sending an ajax request?

Why does the page refresh when sending an ajax request?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-09-19 09:16:381530browse

<code><button class="btn btn-default" onclick="set_chat_msg()">send</button></code>

Click the button to trigger the function

set_chat_msg() part:

<code>var url= "chat_send_ajax.php";
url += "?name=" + strname + "&msg=" + strmsg;//将用户名和消息变量拼接进url中
oxmlHttpSend.open("GET",url,true);//打开请求
oxmlHttpSend.send(null);//发送
//alert(oxmlHttpSend);//为什么在firefox下不注释这里php就无法接收到数据执行呢?chrome是正常的</code>

Then php receives it and processes it.

Why does the page refresh once after I click the button? Then if php cannot receive data at all and will not execute it under firefox, chrome can.

Reply content:

<code><button class="btn btn-default" onclick="set_chat_msg()">send</button></code>

Click the button to trigger the function

set_chat_msg() part:

<code>var url= "chat_send_ajax.php";
url += "?name=" + strname + "&msg=" + strmsg;//将用户名和消息变量拼接进url中
oxmlHttpSend.open("GET",url,true);//打开请求
oxmlHttpSend.send(null);//发送
//alert(oxmlHttpSend);//为什么在firefox下不注释这里php就无法接收到数据执行呢?chrome是正常的</code>

Then php receives it and processes it.

Why does the page refresh once when I click the button? Then if php cannot receive data at all and will not execute it under firefox, chrome can.

As long as you are in the <form> tag, you must clearly specify the type of <button>. Remember to add type="button" or type="submit" to <button> , I’ll be in tears if I talk too much~

Please add the type='button' attribute to the button tag, because the browser will by default think that the attribute of the button is submit.type='submit', and the default behavior of submitting the form will occur.

<code><button type='button' class="btn btn-default" onclick="set_chat_msg()">send</button></code>

W3shool:

Always specify the type attribute for buttons. The default type in Internet Explorer is "button", while the default in other browsers (including the W3C specification) is "submit".

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn