Code for sensitive word review before submitting an article through ajax

Source: Internet
Author: User

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 );
}

}

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.