Get the App
SLTechnology News&Howtos  ›  Development  › 

How to check sensitive words before submitting articles in ajax

Shulou Source: shulou.com Published: 2022-06-03 08:39:25 10月26日 Update

This article mainly introduces how ajax implements the review of sensitive words before submitting the article, which is very detailed and has a certain reference value. Friends who are interested must read it!

So I thought of returning the Boolean value passed by Ajax on submission. In fact, the function get2 () passed by Ajax is a subfunction of chkfull (bt) and cannot be passed to an external function, and alert () does not work. So simply cancel the submit type of the submit button and use Javascript to automatically submit the form after a series of checks.

The code is as follows

HTML

The code is as follows:

/ / Note the type is "button"

/ / embed a hidden text box to pass a value to Ajax

The Javascript part of Ajax

The code is as follows:

Function chkfull (bt) {

Var content = document.getElementById ("usercontent") .value

If (content== "")

{

Alert ("text is required.") / / determine whether the content of the article is empty

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 after xHRObject.onreadystatechange=get2;

{

If (xHRObject.readyState==4 & & xHRObject.status==200)

{

Var serverText=xHRObject.responseText

If (serverText== "sense")

{

If (confirm ("are you sure you want to publish your article with sensitive words?\ n\ r [Yes, publish] will be reviewed, please wait.\ n\ r [No, do not publish] will return to the interface, please modify the words.")

{

Bt.value= "submitting"

Bt.disabled=true;// close button

Document.getElementById ("userchksense") .value = "sense"; / / change the value of userchksense to sense, so that post to add.php can directly check whether to lock the article without having to check the article again.

Document.form1.submit (); / / submit the form

}

Else// seems a little redundant, but it's safer this way.

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/ajax.php", true)

XHRObject.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded")

XHRObject.onreadystatechange=get2;// the get2 here cannot be parenthesized, otherwise the return value of the function is assigned to a variable instead of the function itself to xHRObject.onreadystatechange (Note 1)

XHRObject.send (v)

}

}

The above is all the contents of the article "how to review sensitive words before submitting articles by ajax". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Function article code content button check being article type form no value function interest kernel again variables words actually actually Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Technology Huawei MariaDB vpn