Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2012
    Location
    UK, England
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    [PHP form not waiting to be filled in before being sent]

    Hi

    I am currently building my first website and I have got some issues with the php on my contact form. When I click onto the contact page, it automatically skips to the 'thank-you' page without the form being filled in. So I am getting a message to my email address, but it is obviously empty. I am not sure where the problem is arising, I have tried so many different bits of code - so I am listing the code for the <form> and the code for the <php>

    Hope someone can help, sorry if this a really basic request - just can not seem to work it out. Thanks


    Here is the code I am using for the form

    <div id="full_width_small">

    <form action="New_Form.php" id="thisform" method="post" >


    <p><label for="name">Name:</label><br />
    <input name="name" type="text" id="name" size="40" style="background-color: #D3E0F8" value=""/ ></p>

    <p><label for="email">Email:</label><br />
    <input name="email" type="text" id="email" size="40" style="background-color: #D3E0F8" value="" /></p>

    <p><input type="checkbox" id="remember" name="remember" />
    <label for="remember"><em>Remember this info?</em></label>
    </p>

    </form>
    </div>



    <div id="full_width_small">
    <form action="New_Form.php" id="thisform" method="post">


    <p><label for="message">Message:</label><br />
    <textarea name="message" cols="55" rows="15" id="name" style="background-color: #D3E0F8" value="" /></textarea>
    </p>
    </p>
    <input type="submit" name="submit" value="submit" />
    </form>
    </div>



    Here is the code I am using for the php

    </head>

    <?php

    $EmailFrom = "me@mysite.com";
    $EmailTo = "me@mysite.com";
    $Subject = "Online Form";
    $Name = Trim(stripslashes($_POST['Name']));
    $Email = Trim(stripslashes($_POST['Email']));
    $Comments = Trim(stripslashes($_POST['Message']));

    // validation
    $validationOK=true;
    if (!$validationOK) {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    exit;
    }

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

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

    // redirect to success page
    if ($success){
    print "<meta http-equiv=\"refresh\" content=\"0;URL=thank-you.php\">";
    }
    else{
    print "<meta http-equiv=\"refresh\" content=\"0;URL=something-went-wrong.php\">";
    }


    ?>


    <body>

  • #2
    Regular Coder
    Join Date
    Jan 2012
    Posts
    134
    Thanks
    0
    Thanked 32 Times in 32 Posts
    Is your PHP code on the same page as the HTML for the contact form?

    If so, when you load the page, that PHP code is executing. Since none of the $_POST variables are set, your email sensibly fails to send (edit: or sends you a blank one, in your case)

    Do this:

    PHP Code:
    <?php

    if($_POST) {

    // put your PHP code to process your contact form here

    }

    ?>
    Edit to explain:

    When you open that page, it executes all of the PHP code on it. Since you didn't POST to the page, all of the POST variables are empty. So you get a blank email. If you use if($_POST), you're basically just checking to see if the form was submitted before you actually run that code.
    Last edited by KuriosJon; 01-27-2012 at 03:12 AM.

  • Users who have thanked KuriosJon for this post:

    Pamster (01-27-2012)

  • #3
    New Coder
    Join Date
    Jan 2012
    Location
    UK, England
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank you very much for getting back to me so quickly.

    Your code worked, thanks

    However my form that has been sent to my email is blank - any ideas as to why - Thanks

  • #4
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    yep, your input names are lowercase and your $_POST vars are capitalized...

  • Users who have thanked jmj001 for this post:

    Pamster (01-27-2012)

  • #5
    New Coder
    Join Date
    Jan 2012
    Location
    UK, England
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks, so much

    I am almost there.

    I can now see a message in the 'message' line when I get sent an email, but the 'name' and 'email' are still blank...

    Thanks guys for your help, it is appricated.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That's because you have 2 forms in here. The form the submit button in is the successful one, so only its fields will be successful. Combine the forms into 1.

  • Users who have thanked Fou-Lu for this post:

    Pamster (01-27-2012)

  • #7
    New Coder
    Join Date
    Jan 2012
    Location
    UK, England
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks so much

    Will combine them and see if it works, I suspected that this might be the case.

    Any ideas on how I can visually split my form into two bits - I want the Name and Email in one box and then the Message in a box below it - just so it looks good.

    As I said I am a newbie to this, so at the moment I am working on a trial and error basis, which is time consuming - So I really appricate all the help.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    forms are non-visual elements, so they don't create the display themselves. Use fieldsets or divs to split them up visually.

  • Users who have thanked Fou-Lu for this post:

    Pamster (01-27-2012)

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by Pamster View Post
    // validation
    $validationOK=true;
    if (!$validationOK) {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    exit;
    }
    Delete that, it's all useless. You're setting $validationOK without even checking anything is set. Therefore it will AWAYS be true which renders the next few lines of code useless.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #10
    New Coder
    Join Date
    Jan 2012
    Location
    UK, England
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank you all very much.

    It works - I can now receive messages from my website.

    I can not believe that I have messed around with it for several days, came on here last night and you have all helped me sort it out. BIG BIG Thanks

    I will repost my code, for anyone that has similiar problems - so they can see the code working.....I just need now to work out how to make ensure the sender fills in all the fields - as at the moment, you can click send and you dont need to fill in anything and I dont want to be bombarded with empty emails.

    Thanks once again


  •  

    Tags for this Thread

    Posting Permissions

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