Notes for form submission in ajax post mode ., Ajaxpost
When we create an asynchronous object XMLHttpRequest and transmit data to the background in the post mode at the same time.
Set the value of the xhr. setRequestHeader Member of the asynchronous object
XMLHttpRequest. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); otherwise, the backend cannot receive the value passed in. Because Google Chrome's compiler shows that the pass value
In Request Payload, this is incorrect () -- check the attribute.
The correct method is in Form Data ()
This setting specifies how to encode the form data before it is sent to the server. There are three forms, which can be found in w3c documentation,
Example: <form enctype = "application/x-www-form-urlencoded"> </form>
There is a method serialize () in jquery .. The parameter can be set to a nest. Change to id = "value" & name = "value"
How does ajax post a form?
EncodeURIComponent for the url
The following is a program for submitting forms using post in AJAX.
Like your code, it is a good habit to end or end. the wrong code is like:
If (year. length = 0 ){
Document. getElementById ("txtHint"). innerHTML = "";
Return;
}
Where do you define year in ????????? Change to document. getElementById ("year "). value. At the same time, add an ID attribute to the INPUT. Otherwise, it is ByName. After this is changed, the program runs correctly. Please adopt it. I have passed the local test.
Or change to f. year. value Based on your ideas is also feasible.
Complete executable code:
Function showHint2 (){
Debugger;
Var req;
Var f = document. myform;
If (f. year. value. length = 0 ){
Document. getElementById ("txtHint"). innerHTML = "";
Return;
}
If (window. XMLHttpRequest)
Req = new XMLHttpRequest ();
Else
Req = new ActiveXObject ("Microsoft. XMLHTTP ");
Req. onreadystatechange = function (){
If (req. readyState = 4 ){
If (req. status = 200 ){
Alert ("2 ");
Var meg = req. reponseText;
Document. getElementById ("txtHint"). innerHTML = req. responseText;
}
}
}
Var username = f. username. value;
Var year = f. year. value;
Var post1 = "username =" + username + "& year =" + year;
Var url = "test. php ";
Req. open ("POST", url, true );
Req. setRequestHeader ("CONTENT-TYPE", "applic ...... the remaining full text >>>