简体   繁体   中英

cant figure out php contact form verification woes

Hi I got a contact from script the internet that I have been messing around with, only problem is that the verification that I am trying to add to it just doesn't work. Basically in the form it has name, email, number, type and comment. My main verification woes are with the number field I would like it so that if it is empty it echos "no number" and when the person type in letters instead of numbers it will echo "you need to type with numbers". Something like lol. but I'm stuck. Can any of you geniuses help me? Thanks in advance here is the full code below. ps sorry about previous post i accidently cut off the script:$

<?php

$nowDay=date("d.m.Y");
$nowTime=date("H:i:s");
$subject = "E-mail from my site!";  

if (isset($_POST['submit'])) {

    //contactname
    if (trim($_POST['name'] == '')) {
        $hasError = true;
    } else {
        $name = htmlspecialchars(trim($_POST['name']));
    }
    //emailaddress  
    if (trim($_POST['email'] == '')) {
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i",trim($_POST['name']))) {
        $hasError = true;
    } else {
        $email = htmlspecialchars(trim($_POST['email']));
    }            
    //phonenumber    
    if (trim($_POST['number'] == '')) 
    {
       $fake_num = true;
    }
    else if(!is_numeric($_POST['phonenumber']))
    {
        $fake_num = true;
    }
    else 
    {
        $number = htmlspecialchars(trim($_POST['number']));
    }
    //type
    $type = trim($_POST['type']);

    //comment    
    if (trim($_POST['comment'] == '')) {
        $hasError = true;
    } else {
        $comment = htmlspecialchars(trim($_POST['comment']));
    }               

    if (!isset($hasError) && !isset($fake_num)) {
        $emailTo = 'email@hotmail.com';
        $body = "   Name: $name\n\n
                    Email: $email\n\n
                    Phone number: $number\n\n
                    Type: $type\n\n
                    Comment: $comment\n\n
                    \n This message was sent on: $nowDay at $nowTime";

        $headers = 'From: My Site <'.$emailTo.'>'."\r\n" .'Reply-To: '. $email;
        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }
}
?>


<?php

if(isset($hasError))
{
    echo"<p>form has error</p>";
}
?>

<?php           
if(isset($fake_num))
{
    echo"<p>wrong num</p>";
}
?>

<?php
if(isset($emailSent) && $emailSent == true)
{ 
    echo "<p><strong>Email Successfully Sent!</strong></p>";
    echo "<p>Thank you <strong> $name </strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>";
} 
?>


<div id="stylized" class="myform">
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="bookingform" id="bookingform">
        <h1>Booking form</h1>
        <label>
            Name
            <span class="small"></span>
        </label>
        <input type="text" name="name" id="name" class="required"/>
        <label>
            Your email:
            <span class="small"></span>
        </label>
        <input type="text" name="email" id="email" class="required"/>
        <label>
            Contact Number:
            <span class="small"></span>
        </label>
        <input type="text" name="number" id="number" class="required" />                                                    
        <label>
            Car required:
            <span class="small"></span>
        </label>
        <select name="type" id="type">
            <option selected="selected">Manual</option>
            <option>Automatic</option>
      </select>   
        <label>
            Comment:
            <span class="small"></span>
        </label>
        <textarea cols="" rows="" name="comment" id="comment" class="required"></textarea>                
        <button type="submit" name="submit">Submit</button> 
    </form>
</div>

For checking whether a number has been entered you can use:

if (!preg_match('/^?[0-9]{0,10}$/', $_POST['number'])) {
echo "Please enter a valid number"; //Failed to meet criteria
}

Here you can also specify the amount of numbers that would constitute your valid number with the braces {0,10} , in this case, the number can be up to 11 digits long. If you required it to be only 11 digits long, then use {11} . If you wish to check if a number has been entered at all you can use:

if (empty($_POST['number'])) {
echo "Please enter a valid number"; //Field was left empty
}

PHP does have a built-in email validation function that you can use

filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)

which returns true if the entered email is in the correct format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM