Hard form validation issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    Hard form validation issue

    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_co de" 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($_POS T['check'],"") !="yes"))
    {
    exit();
    }
    if(strcmp($_POS T['check'],"yes"))
    {


    $do = ($_POST['do']);

    if($do == "send")
    {
    $recipient = "secret_mail@so medomain.com";
    $subject = ($_POST['subject']);
    $name = ($_POST['name']);
    $email = ($_POST['email']);
    $message = ($_POST['message']);
    $formsend = mail("$recipien t", "$subject", "$message", "From: $email
    ($name)\r\nRepl y-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...";
    }
    }

    ?>


  • Sjoerd

    #2
    Re: Hard form validation issue

    John wrote:[color=blue]
    > What's wrong with my
    > nice newbie-code? Here it is:[/color]

    This is wrong:[color=blue]
    > if(strcmp($_POS T['check'],"") !="yes"))[/color]

    These are correct:
    if (0 == strcmp($_POST['check'], "yes"))
    if ($_POST['check'] == "yes")
    if (empty($_POST['check'])

    Comment

    • John

      #3
      Re: Hard form validation issue

      > John wrote:[color=blue][color=green]
      > > What's wrong with my
      > > nice newbie-code? Here it is:[/color]
      >
      > Sjoerd <sjoerder@gmail .com> wrote:[color=green]
      > >This is wrong:
      > > if(strcmp($_POS T['check'],"") !="yes"))[/color]
      >
      > These are correct:
      > if (0 == strcmp($_POST['check'], "yes"))
      > if ($_POST['check'] == "yes")
      > if (empty($_POST['check'])
      >[/color]



      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@so medomain.com";
      $subject = ($_POST['subject']);
      $name = ($_POST['name']);
      $email = ($_POST['email']);
      $message = ($_POST['message']);
      $formsend = mail("$recipien t", "$subject", "$message", "From: $email
      ($name)\r\nRepl y-to:$email");

      echo ("<p>Thanks, sucess!</p>");
      }

      }
      else
      {
      echo "You failed to enter secret code, submit cancelled...";
      }
      }

      ?>


      Comment

      Working...