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
Release: 2016-09-19 09:16:38
Original
1526 people have browsed it

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

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

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".

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template