Ajax & PHP Learning by practicing four forms _php tutorial

Source: Internet
Author: User
The previous article has learned how to use Ajax and PHP to read data to a database so that you can dynamically get the latest data from the database. This article continues with the introduction of writing data to the database through forms (form).

When it comes to the question of how to send a request (get and post), the use and distinction of get and post are not explained in detail in this article, generally for web development because the post value is implicit and the amount of data transmitted is large, so it is more commonly used. In this example, the Functions.js is modified to create the XMLHTTP object program as a function Processajax.

Copy to ClipboardWhat to refer to: [www.bkjia.com]function Processajax (serverpage, obj, getOrPost, str) {
Writes the created XMLHttpRequest object to the Getxmlhttp () function, and gets the object
XMLHTTP = Getxmlhttp ();
Get mode (same as previous)
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 mode
else{
The third true parameter opens the async feature
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 (form) pass value
Xmlhttp.send (str);
}
}

The SubmitForm function (functions.js) will be fired when the "Submit" button is clicked, and the function will check if the form content is filled in by the getformvalues function, otherwise it may indicate which item is not filled in. When the check passes, the process_task.php program is called, which writes the form value to the database.

SubmitForm function:

Copy to ClipboardWhat to refer to: [www.bkjia.com]function SubmitForm (theform, Serverpage, ObjID, Valfunc) {
var file = Serverpage;
Check the form value
var str = getformvalues (THEFORM,VALFUNC);
Form all fill in
if (AOK = = True) {
obj = document.getElementById (ObjID);
Run Ajax to pass values
Processajax (serverpage, obj, "post", str);
}
}

Getformvalues function:

function Getformvalues (fobj, Valfunc) {
var str = "";
AOK = true;
var Val;
Traverse all objects in a 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) + "&";
}
Returns the form value as a string
return str;
}

Process_task.php Program:

Copy to ClipboardWhat to refer to: [www.bkjia.com] Require_once ("dbconnector.php");
OpenDatabase ();
Preprocessing of data
$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 INSERT statement
$myquery = "INSERT into task (name, thedate, description) VALUES (' $yourname ', ' $thedate ', ' $yourtask ')";
Execute SQL statement
if (!mysql_query ($myquery)) {
Header ("Location:theform.php?message=there is a problem with the entry.");
Exit
}
Return success Information
Header ("location:theform.php?message=success");
?>

Source: Sample5.rar

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

http://www.bkjia.com/PHPjc/364425.html www.bkjia.com true http://www.bkjia.com/PHPjc/364425.html techarticle The previous article has learned how to use Ajax and PHP to read data to a database so that you can dynamically get the latest data from the database. This article continues with the introduction of the form (...

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