Create an additional field and validate data entry

How to make an additional field in the comments, for example, on the left side there will be a question: “How much is 1 + 1?” and a little to the right of the field for entering the answer, if the user answers 1, then the comment after clicking on the standard “send comment” button is not added, if 2, then the comment is added?

I understand the process itself. Create a data entry field:

 1+1? <input type="text" name="name">

Then we write the condition:

<? 
    if( $_POST['name'] == '2' ){
         echo '<input class="floatl" name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
         <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />';
    }
    else{
        echo '<h1> !</h1>';
    }
?>

In this case, a button to add a comment should appear if the number is entered correctly, but something is wrong here, because the button does not appear.


Answer 1

um… the only problem here is that the extra field appears after the form is loaded and “POST” is not validated. Do the same for the field, just display it at the right time by changing, say, the display css property. Then everything will be fine.


Answer 2

I’m assuming you have a button, but it’s styled float1. Please provide css code for float1. Perhaps it is the whole catch – you just do not see it, but it is on the form.

P.S.: On the other hand, do you think it’s convenient for the user to double-click on add a comment? Is it possible to carry out a check and immediately write a comment to the database?)


Answer 3

ling, tried with get too…

Get won’t work for sure. Plus, replace value="<?php echo $id; ?>"with value="'.$id.'", otherwise it turns out that you php block closes too soon.


Answer 4

why don’t you do it in JavaScript and keep the submit button inactive for clicks until the correct value is entered. do not substitute user-entered data in its place…