Ajax & amp; PHP learning and training 4 forms _ PHP Tutorial

Source: Internet
Author: User
Ajax & amp; PHP: four forms of learning and training. The previous article has learned how to use Ajax and PHP to read data from the database, so that you can dynamically obtain the latest data from the database. This article continues to introduce how to read data from the database through forms (through the previous article, we have learned how to use Ajax and PHP to dynamically obtain the latest data of the database. This article continues to introduce writing data to the database through forms.

When it comes to Form, it involves a request sending method (GET and POST). the usage and difference of GET and POST are not described in detail in this article, generally, it is commonly used in Web development because the POST value is implicit and the amount of data transmitted is large. In this example, modify functions. js to create an XMLHttp object program as a function processajax.

Copy to ClipboardReference: [www.bkjia.com] function processajax (serverPage, obj, getOrPost, str ){
// Write the created XMLHttpRequest object to the getxmlhttp () function and obtain the object.
Xmlhttp = getxmlhttp ();
// GET method (same as previous articles)
If (getOrPost = "get "){
Xmlhttp. open ("GET", serverPage );
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4 & xmlhttp. status = 200 ){
Obj. innerHTML = xmlhttp. responseText;
}
}
Xmlhttp. send (null );
}
// POST method
Else {
// The asynchronous function is enabled for the third true parameter.
Xmlhttp. open ("POST", serverPage, true );
// Create a POST request
Xmlhttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset = GB2312 ");
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4 & xmlhttp. status = 200 ){
Obj. innerHTML = xmlhttp. responseText;
}
}
// Form value passing
Xmlhttp. send (str );
}
}

When you click "Submit" in, the submitform function (functions. js). In this function, the getformvalues function is used to check whether all Form content is filled. Otherwise, the system prompts which Form content is not filled. After the check is passed, the process_task.php program is called, which writes the Form value to the database.

Submitform function:

Copy to ClipboardReference: [www.bkjia.com] function submitform (theform, serverPage, objID, valfunc ){
Var file = serverPage;
// Check the Form value
Var str = getformvalues (theform, valfunc );
// Fill in all forms
If (aok = true ){
Obj = document. getElementById (objID );
// Run Ajax to upload values
Processajax (serverPage, obj, "post", str );
}
}

Getformvalues function:

Function getformvalues (fobj, valfunc ){
Var str = "";
Aok = true;
Var val;
// Traverse all objects in Form
For (var I = 0; I <fobj. elements. length; I ++ ){
If (valfunc ){
If (aok = true ){
Val = valfunc (fobj. elements [I]. value, fobj. elements [I]. name );
If (val = false ){
Aok = false;
}
}
}
Str + = fobj. elements [I]. name + "=" + escape (fobj. elements [I]. value) + "&";
}
// Return the Form value in String format
Return str;
}

Process_task.php program:

Copy to ClipboardReference: [www.bkjia.com] Require_once ("dbconnector. php ");
Opendatabase ();
// Data preprocessing
$ Yourname = strip_tags (mysql_real_escape_string ($ _ POST ['yourname']);
$ Yourtask = strip_tags (mysql_real_escape_string ($ _ POST ['yourtask']);
$ Thedate = strip_tags (mysql_real_escape_string ($ _ POST ['thedate']);
// Create an Insert statement
$ Myquery = "insert into task (name, thedate, description) VALUES ('$ yourname',' $ thedate', '$ yourtask ')";
// Execute the SQL statement
If (! Mysql_query ($ myquery )){
Header ("Location: theform. php? Message = There was a problem with the entry .");
Exit;
}
// Returns the success message.
Header ("Location: theform. php? Message = Success ");
?>

Source code: Sample5.rar

Author Blog: http://www.cnblogs.com/gnielee/

Bytes. This article continues with the introduction of using forms (...

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.