-3

I've done a tiny bit, but it is not so good. I would like to check if email is valid in proper format, name containing no numbers & also how do I add validation to check if the Math question is right. Can anyone please help?

<?php
    $submitted = $_POST["submitted"];
    if($submitted == "true")
    {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $subject = trim($_POST["subject"]);
    $message = trim($_POST["message"]);
    $answerbox = trim($_POST["answerbox"]);
    if($name == "") print "<p>Please type your name.</p>";
    if($subject == "") print "<p>Please type a subject.</p>";
    if($email == "") print  "<p>Please type a valid email address.</p>";
    if($message == "") print "<p>Please type your message.</p>";
    if($answerbox == "") print "<p>Please answer the math question.</p>";
    }

?>
        <form name="contact" action="form2.php" method="post">
            <input type="hidden" name="submitted" value="true"/>
            <label for="YourName">Your Name:</label>
            <input type="text" name="name" class="required" />

            <label for="YourEmail">Your Email:</label>
            <input type="text" name="email" class="required"/>

            <label for="Subject">Subject:</label>
            <input type="text" name="subject" class="required"  />

            <label for="YourMessage">Your Message:</label>
            <textarea  name="message" class="required"></textarea>
            <p class="c3">10 + 5 =<input type="text" name="answerbox" id="answerbox" /></p>

        <fieldset>
            <input type="submit" name="submit" id="submit" value="Send" class="required"/>
            <input type="reset" id="reset" value="Reset"/>      
        </fieldset>

    </form>
Maerlyn
  • 33,687
  • 18
  • 94
  • 85
user2120702
  • 25
  • 1
  • 4
  • 11
  • 1
    Maybe dublicate with http://stackoverflow.com/questions/2146331/php-email-validation-function – Lkopo Aug 27 '13 at 11:31

2 Answers2

1
<?php
    if(isset($_POST['name']))
    {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $subject = trim($_POST["subject"]);
    $message = trim($_POST["message"]);
    $answerbox = trim($_POST["answerbox"]);
    if(strlen($name)<2) {
        print "<p>Please type your name.</p>";
    }else if(strlen($subject)<2) {
        print "<p>Please type a subject.</p>";
    }else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        print  "<p>Please type a valid email address.</p>";
    }else if(strlen($message)<10) {
        print "<p>Please type your message.</p>";
    }else if($answerbox != 15) {
        print "<p>Please answer the math question.</p>";
    }else{
                $headers =  'From: '.$email. "\r\n" .
                            'Reply-To: '.$email . "\r\n" .
                            'X-Mailer: PHP/' . phpversion();
        mail('me@mymail.me',$subject,$message,$headers);
        print "mail succesuffully sent";
    }

}
    ?>

        <form name="contact" action="form2.php" method="post">
            <input type="hidden" name="submitted" value="true"/>
            <label for="YourName">Your Name:</label>
            <input type="text" name="name" class="required" />

            <label for="YourEmail">Your Email:</label>
            <input type="text" name="email" class="required"/>

            <label for="Subject">Subject:</label>
            <input type="text" name="subject" class="required"  />

            <label for="YourMessage">Your Message:</label>
            <textarea  name="message" class="required"></textarea>
            <p class="c3">10 + 5 =<input type="text" name="answerbox" id="answerbox" /></p>

        <fieldset>
            <input type="submit" name="submit" id="submit" value="Send" class="required"/>
            <input type="reset" id="reset" value="Reset"/>      
        </fieldset>

    </form>
TzoLy
  • 70
  • 8
0

The best, safest and easiest way to validate an email is like this:

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // invalid emailaddress
}

As for the other answers (especially the math one) wouldn't you be better off using some javascript on the page directly to validate the answers before you allow the form to be submitted?

The Math can be done like this:

if(empty($answerbox)) 
{
    echo "<p>Please answer the math question.</p>";
}
elseif ($answer!=15)
{
    echo "Oh oh, the answer seems wrong...";
}
else
{
    // Something else?
}

I think you might also be incorrect in your understanding of where and how PHP runs. All the PHP code is executed on the server before it even gets to the user. You can't check an answer through PHP until you submit your form and process it on the server.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • ok..thanks for that!, yah I'm using validate plugin, it works fine, but what if users don't have js enabled? I don't know how to write code in php:( – user2120702 Aug 27 '13 at 11:30
  • In PHP you could do the following assuming that you are pulling the math question out of a database, in which case you should also have the answer in the database. Pass the question ID along in the form and validate that you have the right answer. – Fluffeh Aug 27 '13 at 11:36
  • nope, not using any database, i thought i could check it with php, maybe using some integer function something like that..:P – user2120702 Aug 27 '13 at 11:43
  • I wrote some code in my edit to the answer. – Fluffeh Aug 27 '13 at 11:47