The idea is to return the Boolean value of the Ajax pass at the time of submission. In fact, Ajax-passed function Get2 () is a child function of Chkfull (BT) and cannot be passed to an external function, alert () does not work. So simply cancel the submit button's "Submit" type, and use JavaScript for a series of checks to automatically submit the form.
The code is as follows
Html
Copy Code code as follows:
<input type= "button" class= "Body" id= "SUBM" name= "SUBM" value= "written, posted out"
Onclick= "Chkfull (This)"/>//note that the type is "button"
<input type= "hidden" id= "Userchksense" name= "Userchksense" "value="? />//embed a hidden text box and pass the value to Ajax
The JavaScript part of Ajax
Copy Code code as follows:
function Chkfull (BT) {
var content = document.getElementById ("Usercontent"). Value;
if (content== "")
{
Alert ("Body is required.") //To determine whether the content of the article is empty
return false;
}
Else
{
var Xhrobject=false;
if (window. XMLHttpRequest)//Judge Browser kernel
{
Xhrobject=new XMLHttpRequest ();
}
else if (window. ActiveXObject)
{
xhrobject= new ActiveXObject ("Microsoft.XMLHTTP");
}
function Get2 ()//defines Get2 functions and prepares onreadystatechange. This function cannot be placed at the back of the 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, publish" will be audited, please wait. \n\r "No, do not publish" will return to the interface, please modify the words. "))
{
Bt.value= "being submitted";
bt.disabled=true;//Close button
document.getElementById ("Userchksense"). Value= "sense";//change userchksense value to sense, so that Post to add.php you can directly check to see if you want to lock this article without checking it again
Document.form1.submit ()//Submit this form
}
else//seems a little redundant, but it's safer.
return false;
}
Else
{
Bt.value= "being submitted";
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/ajax.php ", true);
Xhrobject.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
xhrobject.onreadystatechange=get2;//here the Get2 cannot be punched in parentheses, otherwise the function's return value is assigned to a variable, Instead of assigning this function itself to xhrobject.onreadystatechange (Note 1)
Xhrobject.send (v);
}
}