Write data to the database using ajax php

Source: Internet
Author: User

First, you need an input table. Copy codeThe Code is as follows: <! --
To change this template, choose Tools | Templates
And open the template in the editor.
-->
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script src = "submit. js" language = "javascript"> </script>
</Head>
<Body>
Insert knowledge point
<Form name = "insertForm">
<Label for = "question"> </label> knowledge point
<Input name = "question" type = "text"/>
<Br/>
<Label for = "answer"> answer </label>
<Input name = "answer" type = "text"/>
<Br/>
<Br/>
<Input name = "confirm" value = "add" type = "button" onclick = "getValue ();">
</Form>
</Body>
</Html>

Js is required to process the submitted data to the server and obtain the submitted returned data from the server. The submit. js code is as follows:Copy codeThe Code is as follows :/*
* To change this template, choose Tools | Templates
* And open the template in the editor.
*/
Var xmlHttp;
Function getValue (){
Alert ("getvaluel ");
Var question = document. insertForm. question. value;
// Alert (question );
Var answer = document. insertForm. answer. value;
// Alert (answer );
Submit (question, answer );
};
Function submit (question, answer ){
XmlHttp = GetXmlHttpObject ();
If (xmlHttp = null)
{
Alert ("Your browser does not support AJAX! ");
Return;
}
XmlHttp. onreadystatechange = function (){
If (xmlHttp. readyState = 4 ){
Alert (xmlHttp. responseText );
}
};
Var url = "insert1.php ";
XmlHttp. open ("post", url, true );
XmlHttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset = UTF-8 ");
XmlHttp. send ("question =" + question + "& answer =" + answer );

}
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;
}

Then the PHP processing interface is responsible for exchanging data with the server.Copy codeThe Code is as follows: <? Php
/*
* To change this template, choose Tools | Templates
* And open the template in the editor.
*/
// Echo $ _ POST ["question"];
// Echo $ _ POST ["answer"];
$ Q = $ _ POST ['question'];
$ A = $ _ POST ['answer'];
// $ Q = 'qq ';
// $ A = "";
$ Con = mysql_connect ("localhost", "joe", "123 ");
If (! $ Con)
{
// Die ('could not connect: '. mysql_error ());
Echo 'could not connect: '. mysql_error ();
}
Mysql_select_db ("joe", $ con );
Mysql_query ("insert into message VALUES ('$ Q',' $ A', 'None ')");
Mysql_close ($ con );
Echo "input successful ";
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.