Note that there are some explanatory texts on larger screens.

plurals
  1. POcaptcha validation in php using javascript
    primarykey
    data
    text
    <p>I have a comments form which uses ajax to display the comments box using a pop up menu, and on submitting the comment, the comment gets registered to the appropriate post (without any page refresh). I want to add captcha to this form. </p> <p>I tried implementing the following javascript code which generates a small random number captcha.</p> <pre><code>&lt;p&gt; &lt;label for="code"&gt;Write code below &gt; &lt;span id="txtCaptchaDiv" style="color:#F00"&gt;&lt;/span&gt;&lt;!-- this is where the script will place the generated code --&gt; &lt;input type="hidden" id="txtCaptcha" /&gt;&lt;/label&gt;&lt;!-- this is where the script will place a copy of the code for validation: this is a hidden field --&gt; &lt;input type="text" name="txtInput" id="txtInput" size="30" /&gt; &lt;/p&gt; </code></pre> <p>The above html is used to display the generated captcha and a text input for the user to enter the code.</p> <p>The following is the javascript code which generates the code - </p> <pre><code>&lt;script type="text/javascript"&gt; //Generates the captcha function var a = Math.ceil(Math.random() * 9)+ ''; var b = Math.ceil(Math.random() * 9)+ ''; var c = Math.ceil(Math.random() * 9)+ ''; var d = Math.ceil(Math.random() * 9)+ ''; var e = Math.ceil(Math.random() * 9)+ ''; var code = a + b + c + d + e; document.getElementById("txtCaptcha").value = code; document.getElementById("txtCaptchaDiv").innerHTML = code; &lt;/script&gt; </code></pre> <p>The next javascript code is used for validation of the captcha - </p> <pre><code>&lt;script type="text/javascript"&gt; function checkform(theform){ var why = ""; if(theform.txtInput.value == ""){ why += "- Security code should not be empty.\n"; } if(theform.txtInput.value != ""){ if(ValidCaptcha(theform.txtInput.value) == false){ why += "- Security code did not match.\n"; } } if(why != ""){ alert(why); return false; } } // Validate the Entered input aganist the generated security code function function ValidCaptcha(){ var str1 = removeSpaces(document.getElementById('txtCaptcha').value); var str2 = removeSpaces(document.getElementById('txtInput').value); if (str1 == str2){ return true; }else{ return false; } } // Remove the spaces from the entered and generated code function removeSpaces(string){ return string.split(' ').join(''); } &lt;/script&gt; </code></pre> <p>This code works properly when not combined with the comments form. On combining with the comments form, the validation is not done.</p> <p>For the ajax based comments form, the submit button is passing a hidden input variable on submitting the comment which relates it to the appropriate post. This is the submit button code for my comments section - </p> <pre><code>&lt;button type="submit" class="comment-submit btn submit" id="submitted" name="submitted" value="submitted"&gt;&lt;?php _e( 'Submit', APP_TD ); ?&gt;&lt;/button&gt; &lt;input type='hidden' name='comment_post_ID' value='&lt;?php echo $post-&gt;ID; ?&gt;' id='comment_post_ID' /&gt; </code></pre> <p>So basically i want my code to check the captcha value first on submit button of the comment form, and if its proper i want to submit the comment using the ajax feature only. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload