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 ClipboardQuoted 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.
submitform function:
Copy to ClipboardQuoted 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 ClipboardQuoted 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 (...