Therefore, a Boolean value passed by Ajax is returned during submission. In fact, the get2 () function passed by Ajax is a subfunction of chkfull (bt) and cannot be passed to external functions. alert () does not work either. Therefore, the "Submit" type of the "Submit" button is simply canceled, and the form is automatically submitted after a series of checks are performed using Javascript.
The Code is as follows:
HTML
Copy codeThe Code is as follows:
<Input type = "button" class = "body" id = "subm" name = "subm" value = "written, published"
Onclick = "chkfull (this)"/> // note that the type is "button"
<Input type = "hidden" id = "userchksense" name = "userchksense" value = "? "//> // Embed a hidden text box and send a value to Ajax.
Javascript section of Ajax
Copy codeThe Code is as follows:
Function chkfull (bt ){
Var content = document. getElementById ("usercontent"). value;
If (content = "")
{
Alert ("body is required. "); // Determines whether the content of the article is null.
Return false;
}
Else
{
Var xHRObject = false;
If (window. XMLHttpRequest) // judge the browser kernel
{
XHRObject = new XMLHttpRequest ();
}
Else if (window. ActiveXObject)
{
XHRObject = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Function get2 () // defines the get2 function to prepare for onreadystatechange. This function cannot be placed behind xHRObject. onreadystatechange = get2;
{
If (xHRObject. readyState = 4 & xHRObject. status = 200)
{
Var serverText = xHRObject. responseText;
If (serverText = "sense ")
{
If (confirm ("your article has sensitive words. Are you sure you want to publish it? \ N \ r [Yes, release] will be reviewed. Please wait. \ N \ r [No, do not publish] will return to the interface. Modify the words. "))
{
Bt. value = "submitting ";
Bt. disabled = true; // close the button
Document. getElementById ("userchksense "). value = "sense"; // change the value of userchksense to sense. In this way, post to add. php can directly check whether to lock this article, instead of checking the article again
Document. form1.submit (); // submit the form
}
Else // it seems redundant, but it is relatively safe.
Return false;
}
Else
{
Bt. value = "submitting ";
Bt. disabled = true;
Document. getElementById ("userchksense"). value = "nonsense ";
Document. form1.submit ();
}
}
}
Var title = document. getElementById ("usertitle"). value;
Var tag = document. getElementById ("usertag"). value;
Var intro = document. getElementById ("userintro"). value;
Var content = document. getElementById ("usercontent"). value;
Var v = "title =" + title + "& tag =" + tag + "& intro =" + intro + "& content =" + content;
XHRObject. open ("POST", "../ajax. php", true );
XHRObject. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
XHRObject. onreadystatechange = get2; // get2 cannot be enclosed in parentheses. Otherwise, the return value of the function is assigned to a variable instead of the function itself being assigned to xHRObject. onreadystatechange (Comment 1)
XHRObject. send (v );
}
}