Ajax implementation of the article before submitting the sensitive Word audit Code _ajax related

Source: Internet
Author: User
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);
}

}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.