Home > Backend Development > PHP Tutorial > Ajax & PHP Learning and Practicing Part 4 Form_PHP Tutorial

Ajax & PHP Learning and Practicing Part 4 Form_PHP Tutorial

WBOY
Release: 2016-07-21 14:55:17
Original
909 people have browsed it

Through the previous article, you have 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 writing data into the database through forms.

When talking about Form, it involves the issue of sending request methods (GET and POST). The use and difference between GET and POST will not be explained in detail in this article. Generally, for web development, the POST value transfer is implicit. And the amount of data transmitted is large, so it is more commonly used. In this example, make the following modifications to functions.js, and create the XMLHttp object program as a function processajax.

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] function processajax (serverPage, obj, getOrPost, str){
//Write the created XMLHttpRequest object into the getxmlhttp() function and obtain the object
xmlhttp = getxmlhttp ();
//GET method ( Same as the 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 third true parameter will turn on the asynchronous function
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 pass value
xmlhttp.send(str);
}
}

In the picture below, when the "Submit" button is clicked, the submitform function (functions.js) will be triggered. In this function, the getformvalues ​​function will be used to check whether the Form content has been filled in. Otherwise, it will prompt which item is not filled in. When the check passes, the process_task.php program will be called, which will write the Form value to the database.

Ajax & PHP Learning and Practicing Part 4 Form_PHP Tutorial

submitform function:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] function submitform (theform, serverPage, objID, valfunc){
var file = serverPage;
//Check the Form value
var str = getformvalues(theform,valfunc);
//Fill all the Form
if (aok == true){
obj = document.getElementById(objID);
//Run Ajax to pass value
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 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 as String
return str;
}

process_task.php program:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] require_once ("dbconnector.php");
opendatabase();
//Preprocess data
$yourname = strip_tags (mysql_real_escape_string ($_POST['yourname']) ;
$myquery = "INSERT INTO task (name, thedate, description) VALUES ('$yourname','$thedate','$yourtask')";
//Execute SQL statement
if (! mysql_query ($myquery)){
header ("Location: theform.php?message=There was a problem with the entry.");
exit;
}
//Return success message
header ("Location: theform.php?message=Success");
?>
Source code: Sample5.rar

Author’s blog: http://www.cnblogs.com/gnielee/

http://www.bkjia.com/PHPjc/364425.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364425.htmlTechArticleThrough the previous article, you have learned how to use Ajax and PHP to read data from the database, so that it can be dynamically Get the latest data from the database. This article continues to introduce the form (...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template