Grow your CSS skills. Land your dream job.

PHP Contact – does not validate form in Oprea & Chrome

  • # April 2, 2010 at 8:21 pm

    Hi,

    I was using the PHP contact form that I download from CSS Tricks. When the user clicks submit in Opera or Chrome it submits the form even if the text fields are blank -

    Any help with this would be appreciated

    John

    # April 3, 2010 at 7:46 am

    To make sure all the fields have a value, you’d need to run something like:

    Code:
    if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])){
    //run code here
    die()
    } else {
    //continue with validation
    }

    That basically says "if a field is empty, run code & die (stop processing), else continue with validation"

    # April 5, 2010 at 10:53 am

    Hi,

    Thanks for the relpy. That makes sense but I am not so versed with PHP, so I am not sure how to get the code to work. Could you help me with getting the validation to work?

    Below is the code that I am using right now

    John

    Code:
    < ?php

    // CHANGE THE VARIABLES BELOW

    $EmailFrom = "EAM Contact";
    $EmailTo = "eamartist@yahoo.com";
    $Subject = "Email Contact Form";

    $Name = Trim(stripslashes($_POST['Name']));
    $City = Trim(stripslashes($_POST['City']));
    $Email = Trim(stripslashes($_POST['Email']));
    $Message = Trim(stripslashes($_POST['Message']));

    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "n";
    $Body .= "City: ";
    $Body .= $City;
    $Body .= "n";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "n";
    $Body .= "Message: ";
    $Body .= $Message;
    $Body .= "n";

    // send email
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>“);

    // redirect to success page
    // CHANGE THE URL BELOW TO YOUR “THANK YOU” PAGE
    if ($success){
    print ““;
    }
    else{
    print ““;
    }
    ?>

    # April 6, 2010 at 11:47 am
    Code:
    < ?php

    // CHANGE THE VARIABLES BELOW

    $EmailFrom = "EAM Contact";
    $EmailTo = "eamartist@yahoo.com";
    $Subject = "Email Contact Form";

    $Name = Trim(stripslashes($_POST['Name']));
    $City = Trim(stripslashes($_POST['City']));
    $Email = Trim(stripslashes($_POST['Email']));
    $Message = Trim(stripslashes($_POST['Message']));

    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "n";
    $Body .= "City: ";
    $Body .= $City;
    $Body .= "n";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "n";
    $Body .= "Message: ";
    $Body .= $Message;
    $Body .= "n";

    foreach($_POST as $key => $value){
    if(empty($value)){
    echo “You have left the ” . $key . ” field blank.”;
    die();
    }
    }

    // send email
    $success = mail($EmailTo, $Subject, $Body, “From: < $EmailFrom>“);

    // redirect to success page
    // CHANGE THE URL BELOW TO YOUR “THANK YOU” PAGE
    if ($success){
    print ““;
    }
    else{
    print ““;
    }
    ?>

    That should work. It is untested, but try it and get back to me.

    # April 6, 2010 at 1:35 pm

    Hi,

    That works great.

    I just realized that the PHP form has a bunch of JavaScript that is supposed to do the validation.

    This is a link – its the second form http://css-tricks.com/downloads/php-stuff/

    Anyway – thanks for the help – if you have time, I have a style quesiton – When the warning happens is there a way to create that on the same page or at least a pop up -

    John

    # April 6, 2010 at 2:59 pm
    "ponyack" wrote:
    Hi,

    That works great.

    I just realized that the PHP form has a bunch of JavaScript that is supposed to do the validation.

    This is a link – its the second form http://css-tricks.com/downloads/php-stuff/

    Anyway – thanks for the help – if you have time, I have a style quesiton – When the warning happens is there a way to create that on the same page or at least a pop up -

    John

    Hey ponyack,

    You would either have to use JavaScript to validate the fields (but if JS is disabled, that will fail) or you’re going to have to use AJAX to check for the PHP reply and show it on the screen. Both of which I don’t have much experience in :p

    # April 8, 2010 at 10:14 am

    Hi,

    Thanks for the help, I have found another PHP script that does a good job of validation.

    John

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".