This is a discussion on Hard form validation issue within the PHP Language forums, part of the PHP Programming Forums category; I have a mail form, where I would like the users to enter a secret code and check one checkbox ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I have a mail form, where I would like the users to enter a secret code and
check one checkbox before the form are processed and the values can mailed. Otherwise stop and display an error. But it won't work. What's wrong with my nice newbie-code? Here it is: The form: <div> <form method="post" action="process.php"> <p>Name:</p> <input name="name" type="text" /> <p>Email:</p> <input name="email" type="text" /> <p>Subject:</p> <input name="subject" type="text" /> <p>Message:</p> <textarea name="message" rows="6"></textarea> <p>Enter secret send-code:</p> <input name="secret_code" type="text" /> <p>Agree to terms?:</p> Yes <input name="check[]" type="checkbox" value="yes" /> No <input name="check[]" type="checkbox" value="no" /> <br /> <input type="submit" name="submit" value="Send" /><input type="hidden" name="do" value="send" /><input type="reset" name="reset" value="Reset" /> </form> </div> Here is process.php: <?php //validating input fields and checkbox, secret code is SECRET and required checkbox value has to be "yes" if (empty($_POST['secret_code'])) { exit(); } if (strcmp($_POST['secret_code'],"SECRET")) if(strcmp($_POST['check'],"") !="yes")) { exit(); } if(strcmp($_POST['check'],"yes")) { $do = ($_POST['do']); if($do == "send") { $recipient = "secret_mail@somedomain.com"; $subject = ($_POST['subject']); $name = ($_POST['name']); $email = ($_POST['email']); $message = ($_POST['message']); $formsend = mail("$recipient", "$subject", "$message", "From: $email ($name)\r\nReply-to:$email"); echo ("<p>Thanks, success!</p>"); } } else { echo "You failed to enter secret code, or you didn't agree to the terms, submit cancelled..."; } } ?> |
|
|||
|
John wrote:
> What's wrong with my > nice newbie-code? Here it is: This is wrong: > if(strcmp($_POST['check'],"") !="yes")) These are correct: if (0 == strcmp($_POST['check'], "yes")) if ($_POST['check'] == "yes") if (empty($_POST['check']) |
|
|||
|
> John wrote:
> > What's wrong with my > > nice newbie-code? Here it is: > > Sjoerd <sjoerder@gmail.com> wrote: > >This is wrong: > > if(strcmp($_POST['check'],"") !="yes")) > > These are correct: > if (0 == strcmp($_POST['check'], "yes")) > if ($_POST['check'] == "yes") > if (empty($_POST['check']) > I tried to rewrite the process.php and also add another checkbox to be cheked, but still something is wrong. Here is the code, can you see the error in the code? <?php //check if secret code is not filled in and if checkbox are not cheked if (empty($_POST['secret_code']) && (empty($_POST['check'])) //is so then exit { exit(); } //check if secret code are submitted and if it matches if (strcmp($_POST['secret_code'],"SECRET")) //check if checkbox are cheked "yes" //and if checkbox 2 are cheked "yes" if (0 == strcmp($_POST['check'], "yes")) if (0 == strcmp($_POST['check2'], "yes")) if ($_POST['check'] == "yes") && ($_POST['check2'] == "yes") //then start the mailing process { $do = ($_POST['do']); if($do == "send") { $recipient = "secret_mail@somedomain.com"; $subject = ($_POST['subject']); $name = ($_POST['name']); $email = ($_POST['email']); $message = ($_POST['message']); $formsend = mail("$recipient", "$subject", "$message", "From: $email ($name)\r\nReply-to:$email"); echo ("<p>Thanks, sucess!</p>"); } } else { echo "You failed to enter secret code, submit cancelled..."; } } ?> |
![]() |
| Thread Tools | |
| Display Modes | |
|
|