Ask: PHPjs implements complex button functions. ask prawn: I want to create a button in the webpage. click this button to implement the following functions: first, determine whether the name given on the page already exists in the SQL database. If it exists, a prompt box is displayed and then jumps out. If it does not exist, you need to store the content on the page (such as name, gender, Email, phone number, etc.) into the SQL database, and then prompt that the content has been written into the database, and then jump to another page. The buttons I have defined are as follows (a. Ask: PHP js implements complicated button functions
For more information, see the following figure:
First, determine whether the name given on the page already exists in the SQL database. If it exists, a prompt box is displayed and then jumps out. If it does not exist, you need to store the content on the page (such as name, gender, Email, phone number, etc.) into the SQL database, and then prompt that the content has been written into the database, and then jump to another page.
The buttons I have defined are as follows (a. php file ):
Then we plan. function data_send () of the js file to implement the button function. Currently, I know how to implement the prompt box and webpage jump, but how to determine whether to write data to the database and how to write data to the database in js, I don't know.
I am a newbie. I hope you will give me more advice. thank you for your help ~!
------ Solution --------------------
Ajax detection and warehouse receiving, and then jump
------ Solution --------------------
Using ajax to request php, php checks whether the name exists.
------ Solution --------------------
I have recently studied and shared the following:
File 1, (conn. php) code is as follows:
$ Conn = mysql_connect ('localhost', 'database username', 'database password ');
Mysql_select_db ('database name', $ conn );
?>
The code for file 2、(a.html) is as follows:
Script
Var xmlHttp
Function check (str ){
XmlHttp = GetXmlHttpObject ()
If (xmlHttp = null)
{
Alert ("Browser does not support HTTP Request ")
Return
}
Var url = "a. php"
Url = url + "? N = "+ str
Url = url + "& sid =" + Math. random ()
XmlHttp. onreadystatechange = stateChanged
XmlHttp. open ("GET", url, true)
XmlHttp. send (null)
}
Function stateChanged ()
{
If (xmlHttp. readyState = 4
------ Solution --------------------
XmlHttp. readyState = "complete ")
{
Document. getElementById ("name"). innerHTML = xmlHttp. responseText
}
}
Function GetXmlHttpObject ()
{
Var xmlHttp = null;
Try
{
// Firefox, Opera 8.0 +, Safari
XmlHttp = new XMLHttpRequest ();
}
Catch (e)
{
// Internet Explorer
Try
{
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP ");
}
Catch (e)
{
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
}
Return xmlHttp;
}
Script
File 3 (a. php) code is as follows:
Include "conn. php ";
$ SQL = 'select * from user'; // The user name of your database. modify it by yourself.
$ Res = mysql_query ($ SQL );
While ($ row = mysql_fetch_assoc ($ res )){
$ U_name [] = $ row ['name']; // name in $ row ['name'] is the name of your database field
}
$ Name = $ _ GET ['n'];
If (in_array ($ name, $ u_name )){
Echo "this user name has been registered. Please enter a new user name! ";
} Else {
Echo "available ";