Required fields in PHP Forms

Discussion in 'PHP' started by gerdywerdy, Mar 8, 2009.

  1. #1
    I have a pretty simple form in php but my problem is people can leave it blank and it still goes through when i hit submit

    I want to make the fields required and error messages to show when they dont.

    i have one working for email
    if something is entered in email it can be cecked to make sure is correct format but if its left blank it goes through

    hopefully somebody can help

    thanks
    Ger

    <?php

    $ip = $_POST['ip'];
    $qkemail = $_POST['qkemail'];
    $attn = $_POST['attn'];
    $qkname = $_POST['qkname'];
    $qkcitfr = $_POST['qkcitfr'];
    $qkcountryfr = $_POST['qkcountryfr'];
    $qkcitto = $_POST['qkcitto'];
    $qkcountryto = $_POST['qkcountryto'];
    $qkcube = $_POST['qkcube'];
    $qkmfdate = $_POST['qkmfdate'];
    $qkmfmonth = $_POST['qkmfmonth'];
    $qkmfyear = $_POST['qkmfyear'];

    if(!$qkemail == "" && (!strstr($qkemail,"@") || !strstr($qkemail,".")))
    {
    echo "<h2>Use Back - Enter valid e-mail</h2>\n";
    $badinput = "<h2>Feedback was NOT submitted</h2>\n";
    echo $badinput;
    die ("Go back! ! ");
    }

    $todayis = date("l, F j, Y, g:i a") ;
    $attn = $attn ;
    $subject = $attn;

    $message = " $todayis [EST] \n

    Name: $qkname \n

    \n
    The Customer is moving from \n
    $qkcitfr \n
    $qkcountryfr \n

    \n
    The Customer is moving to \n
    $qkcitto \n
    $qkcountryto \n

    \n
    The customer is moving a total of $qkcube cubes on or around $qkmfdate/$qkmfmonth/$qkmfyear \n
    ";

    $from = "From: $qkemail\r\n";


    mail("info@my domain.com", $subject, $message, $from);

    ?>

    <p align="center">
    Date: <?php echo $todayis ?>
    <br />
    Thank You : <?php echo $qkname ?> ( <?php echo $qkemail ?> )
    <br />

    We will be in touch shortly

    <br /><br />
    <a href="index.html"> Next Page </a>
    </p>

    </body>
    </html>
     
    gerdywerdy, Mar 8, 2009 IP
  2. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You have to validate each field. Not only to see if it was filled out or not, but also so malicious data isn't submitted.

    Use preg_match with an if clause.
     
    qualityfirst, Mar 8, 2009 IP
  3. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #3
    2 great functions - isset and preg_match ( both can be found on php.net ). Find them, learn them, add them = problem solved ;)
     
    ActiveFrost, Mar 8, 2009 IP
  4. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Beware of isset though. A field with just a space entered will count as being "set", so you have to always use preg_match afterwards.
     
    qualityfirst, Mar 8, 2009 IP
  5. gerdywerdy

    gerdywerdy Member

    Messages:
    381
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    35
    #5
    thanks guys let me check it out

    i am just beginnging with php so hopefully wont be too tough
     
    gerdywerdy, Mar 8, 2009 IP