Ajax+PHP Learning and Practicing Part 4 Form_PHP Tutorial

WBOY
Release: 2016-07-21 15:42:11
Original
698 people have browsed it

When it comes to 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, POST value transmission 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 code The code is as follows:

function processajax (serverPage, obj, getOrPost, str){
//will Create an XMLHttpRequest object and write it 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 The parameters will open the asynchronous function
xmlhttp.open("POST", serverPage, true);
//Create 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 contents have been filled in. Otherwise, it will prompt which item has not been filled in. When the check passes, the process_task.php program will be called, which will write the Form value to the database.
notask
submitform function:
Copy code The code is as follows:

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:
Copy code The code is as follows:

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 as String
return str;
}

process_task.php program:
Copy code The code is as follows:

require_once ("dbconnector. php");
opendatabase();
//Preprocess 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 an 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 was a problem with the entry.");
exit;
}
//Return success message
header ("Location: theform.php?message =Success");
?>

Source code download

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321007.htmlTechArticleWhen talking about Form, it involves an issue of sending request methods (GET and POST). The use of GET and POST and The difference will not be explained in detail in this article. Generally, for web development, the POST value transfer is implicit...
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