Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Contact.php problems

  1. #1
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Contact.php problems

    Hello all I`m having problems with a contact.php file.

    I have made up the contact form and seams to work fine but anytime anything is or isnt filled in the form goes straight to the thankyou page, which is going to be a problem because even if the visitor hasn`t filled the form in it`ll go to the thankyou page, i have tried adding different ways to stop this from happening but as i`m a novice i dont know what to do next.

    Can anyone help me out?

    Thanks

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    There are many ways to accomplish form validation - one of them being with php and another being javascript.

    PHP Code:
    <?php
    if(isset($_POST['submit'])){ //check to see if the page was submitted
        
    $errors false//always assume your user has no errors from the beginning
        
        
    if(empty($_POST['name'])){ //check if the name field was empty
            
    $errors['name'][] = "Name field was empty.";
        }
        if(
    preg_match("/^[a-zA-Z]+$/"$_POST['name'])){ //if the name has other letters then a-z or A-Z
            
    $errors['name'][] = "Name must contain only letters from a-z and A-Z";
        }
        if(empty(
    $_POST['subject'])){
            
    $errors['subject'][] = "Subject field was empty";
        }
        if(
    preg_match("/^[a-zA-Z]+$/"$_POST['subject'])){
            
    $errors['subject'][] = "Name must contain only letters from a-z and A-Z";
        }
        if(
    count($errors) > 0){ //if there were errors in the $errors variable
            
    foreach($_POST as $key => $value){
                
    $errors[$key]['value'] = $value;
            }
        } else {
            
    // NO ERRORS
        
    }
        
    } else { 
    //if the page didn't get a post key with the name 'submit'
        
    echo "No information submitted.";
    }
    This is how I would approach it in PHP. Check everything you need and then put it in a multidimensional that has the errors. Then, check for errors. If there are errors, add the value of each field to it's own array so that the form doesn't empty when the user fixes his problems. Output all the errors within every array of the parent array that dont have the key 'value'
    Jeremy | jfein.net

  3. #3
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi thanks alot for replying i really appreciate your help,

    I tried what you gave but it didnt seam to work, It may have something to do with the way the actual for is set up with the name for instance is inside the form instead of out of it if you know what i mean?

    here is the code for html and php

    HTML Code:
                  <form id="form1" name="form1" method="post" action="http://www.#####.co.uk/contact.php">
                    <p>For a free, no obligation quote, simply fill in the the short form below:</p>
                    <p><input name="fullname" type="text" class="default-value" value="Full Name:" /></p>
                    <p><input name="businessname" type="text" class="default-value" value="Business Name:" /></p>
    				<p><input name="address" type="text" class="default-value" value="Address:" /></p>
    				<p><input name="postcode" type="text" class="default-value" value="Postcode:" /></p>
    				<p><input name="emailaddress" type="text" class="default-value" value="Email Address:" /></p>
                    <input name="phonenumber" type="text" class="default-value" value="Phone Number:" /></p>
                    <p><textarea name="quotedetails" id="textarea" cols="45" rows="5" class="default-value">Additional Details:</textarea></p>
    				<p><input name="verif_box" type="text" id="verif_box" class="default-value" value="Please Describe The Image:" /></p>
                    <p><img src="verificationimage.php?%3C?php%20echo%20rand(0,9999);?%3E" alt="PLEASE DESCRIBE THE IMAGE" /></p>
                    <p>                
                      <input type="submit" name="Click for Quote" value="Click for Quote" /> 
                      <input type="reset" name="reset" value="Reset" />
                    </p>
                  </form>
    HTML Code:
    <?php
    $fullname = $_POST["fullname"];
    $businessname = $_POST["businessname"];
    $address = $_POST["address"];
    $postcode = $_POST["postcode"];
    $emailaddress = $_POST["emailaddress"];
    $phonenumber = $_POST["phonenumber"];
    $quotedetails = $_POST["quotedetails"];
    $verif_box = $_POST["verif_box"];
    
    $to = "info@#####.co.uk";
    $subject = "enquiry";
    $headers = "From: $emailaddress\n";
    header("location:http://www.#####.co.uk/thankyou.html");
    
    
    $message = "A visitor to your site has filled out the following information.\n
    Full Name: $fullname
    Business Name: $businessname
    Company Address: $address
    Post Code: $postcode
    Email Address: $emailaddress
    Phone Number: $phonenumber
    Quote Details: $quotedetails ";
    
    if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
    mail($to,$subject,$message,$headers);
    }
    
    ?>
    Hopefully you can help me out as i dont really know what i`m doing, thanks again

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Yeah - I don't know if that's the best idea though.

    You can take a look at the source of this page: http://unlinkthis.net/scripts/user-login/contact.php (which for some reason does not have validation so dont send me empty emails, please!)

    AND: http://css-tricks.com/video-screenca...-contact-form/

    But don't put the field labels inside of the actual field.
    Jeremy | jfein.net

  5. #5
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi thanks for the reply again,

    I wanted to keep it like that with the text inside the field for the look of my site, as i`d have to totally change the site to make the contact form fit, It`s on every page of the site because of the nature of the site and not just on a contact page, i think this is why i`m having the problems because of the text being inside the form.

    Is there any other way to do this with the text inside the form?

    Thanks

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You could, instead of using the empty() function, use a comparison operator (==)

    So:
    Code:
        if(empty($_POST['fullname']) || $_POST['fullname'] == "Full Name:"){ //check if the name field was empty 
            $errors['fullname'][] = "Name field was empty/unchanged."; 
        }
    But again, I would still suggest a better interface.
    Last edited by Nile; 02-04-2011 at 10:45 PM.
    Jeremy | jfein.net

  7. #7
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi i`ve just tried that but it still goes to the thankyou page if everything is left blank, it`s really frustrating, seams like i`ve tried everything.

    any other options?

    Thanks again

  8. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You have to make sure this code is in contact.php - that's where it is, correct?

    If I were doing this, I would combine the html with contact.php so that if there were fields that were invalid you could easily say which ones they were without having to set a cookie and back up.
    Jeremy | jfein.net

  9. #9
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi, yeah i placed the code below Quote Details: $quotedetails "; when you say combine the html with contact.php do you mean just place the whole html page inside the php file? (above it)

    Thanks again

  10. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    No - place it after the PHP.
    PHP Code:
    <?php

    // PHP CODE HERE

    ?>

    <!--
      HTML HERE
    -->
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •