treehouse : what would you like to learn today?
Web Design Web Development iOS Development

PHP Form Question(s)

  • Please let me know if this is not appropriate for CSS-Tricks.

    I have 2 PHP forms on my website, the results for each form are to be e-mailed. Upon submit, on both forms, I get the message that the requests have been e-mailed successfully, and actually get the email. The trouble is, the emails are empty.

    I guess it is safe to assume that the form.php files are located correctly on the server(?) They aren't in any special place,but residing in my root folder and identified by the server as being "application/x-httpd-php" files.

    I haven't configured any "mail-handler" application. The server supports cgimail and has it installed I thought I had better stay with the php script because it seems to "halfway" work. Where do I go from here to resolve my dilemma?
  • Something is wrong with your _POST _GET areas
  • Geez! I'm such a noob. Let me check it out. Briefly looking at it, I think I know where the problems lie. Thanks Steven.
  • Here is what I'm looking at, no _GET anywhere, I expanded on what I needed in my form after watching a video tutorial...... The following is from the smaller files that captures input from a simple email form.


    <?php

    /* Subject & Email Variables for Quote Form*/

    $emailSubject = ' Request for Information' ;
    $webMaster = ' ken@xxxxxxxx.com' ;


    /* Gathering Data Variables for Quote Form*/

    $email = $_POST [ 'email' ] ;
    $myQuestions = $_POST [ 'myQuestions' ] ;


    /* Email Structure of Form Information from Quote Form */

    $body = <<<EOD
    <br><hr><br>
    Email Address: $email <br>
    My Questions : $myQuestions <br>
    EOD;

    $headers = "From : $email\r\n ";
    $headers .= "Content-type: text/html\r\n " ;
    $success = mail ( $webMaster, $emailSubject, $body, $headers ) ;

    /* Results rendered as HTML */

    $theResults = <<<EOD

    ..........followed by HTML for my site's header, body, footer, etc

    </body>
    </html>
    EOD;
    echo "$theResults";

    ?>
  • This is the string I am questioning:

    $success = mail ( $webMaster, $emailSubject, $body, $headers ) ;

    I'm using "mail" because it is what was used in the tutorial, and have been in search of clearing this up for a couple of hours .. to no avail. I'm assuming, unless there is something glaring that I should be embarrassed about, this 'mail' string might be the culprit?
  • tried it without "$success =" ?
  • Thanks for your reply, and willingness to help me out.

    I commented out the line in question, no e-mail is send at all. As stevendeeds suggested, am I omitting _GET somewhere? I admit I am lost here, but am keeping a stiff upper lip about it. :-) I've looked at other examples, and have seen no reference to _GET (?)
  • I use a form myself that works perfectly but the code is a bit different from yours. Seems to work pretty well. Used it on a few occasions. The way I do it is have the form on a page say contact.php and have the form send to sendemail.php. This is the script inside sendemail.php. Obviously you can modify it and include the rest of your site design around it.


    <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    if(!$email == "" && (!strstr($email,"@") || !strstr($email,".")))
    {
    echo "<h3>Please enter a valid email address in the format: user@domain.com</h3>\n";
    $badinput = "<h3>Your query has not been sumbitted.</h3>\n";
    echo $badinput;
    die ("Press back on your browser and try again.");
    }

    if(empty($name) || empty($email) || empty($message )) {
    echo "<h2>You did not enter information in all the fields.</h2>\n";
    die ("<h2>Press back on your browser and try again.</h2>");
    }

    $subject = "whatever you want the subject";
    $message = "Date: $todayis \n
    From: $name (Email: $email) \n
    Message: $message \n
    ";
    $from = "From: $email\r\n";

    mail("your@address.here", $subject, $message, $from);
    ?>

    <p>
    Thank you, your request has been submitted successfully.</p>
  • Thanks AWKM, I appreciate your tenacity in trying to help me solve this problem. Let me check it out. I'm not concerned with the differences in code at this point, if it works for you, I'm willing to give it a try.

    In my searching for a solution I have run across many people with the same problem as I have. The video tutorial was a popular tutvids tutorial, that seemingly drew a large crowd. I'm actually surprised, his videos are generally spot on.

    I'll be back to report! Thanks again!
  • I don't know, not having too much luck with the coding you provided above AWKM. I fail to get a returned e-mail, I will toy around with it further. Naturally, I had to modify it for the values I have in my form, etc.

    I'm thinking, I'm not married to php in getting something suitable to bring form data into an e-mail. I may have ta abandon php, and look for alternatives. Cgi-email I believe can do the same thing, minus the php scripting. I thought, since I am also working on a WP blog for my site, the continued exposure to php would aid me in learning more about php.

    If anyone has $.02 they would like to share, I'm open. If I need to, I can share my actual html forms ................ I find it difficult to believe that with all this talent on the forums, someone hasn't stepped forward with a "tried and true work-around. ;-)

    This saga will continue ....

  • I would start by commenting out the part that sends the email and just echoing out all of your variables from the form.

    Heres a very stripped down version of an email sent in PHP.

    Hope this helps to get you on the right track.


    <?
    // Variables from form. Im not really using these anywhere in the email below. I just set them as an example.
    $email = $_POST['email'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $password = sha1($_POST['password']);
    $city = $_POST['city'];
    $state = $_POST['state'];
    // subject
    $subject = 'Thanks for signing up!';

    // HTML message
    $message = '
    <html>
    <head>
    <title>' . $_POST['firstname'] . ' signed up for RYFO.</title>
    </head>
    <body>
    <p style="font-size: 19px;">' . $_POST['firstname'] . ',</p>
    <p style="font-size: 19px;"> Thanks for signing up.</p>
    <p style="font-size: 19px;">Your login credentials are </p>
    <p style="font-size: 19px;"> Username: ' . $_POST['email'] . '<br />Password: ' . $_POST['password'] . '</p>
    <p style="font-size: 19px;"> We hope to see you around the community.</p>
    </body>
    </html>
    ';

    // To send HTML mail, the Content-type header must be set
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= 'To: ' . $_POST['firstname'] . ' <' . $_POST['email'] . '>' . "\r\n";
    $headers .= 'From: RYFO <contact@ryfo.org>' . "\r\n";

    // Mail it
    mail($to, $subject, $message, $headers);
    ?>

  • I'd like to see your HTML just to see the naming. It may be that simple. I've looked over code for hours before and found that I'd named something wrong.
  • Please post the HTML as we need to see if you're passing the form variables correctly
  • Here's the HTML, sorry I've been late in getting back to see all this activity, I've been scouring the web all morning, been up since 5AM:

    Ah, how do I post HTML without having the coding stripped away?
  • Hmmm. That didnt post the HTML. Surround your code with
    <pre><code>












  • Try posting it at http://www.pastebin.com then giving us the link.
  • Here you go, thanks for your patience!

    http://pastebin.com/3wtqQxJm
  • OK, it has to do with your enctype. Lemme get an example mocked up for you.
  • I appreciate your time guy.
    This is the smaller of the two forms I've employed.

    Sorry for the mess I've made of the coding, looks pretty embarrassing in pastebin
  • Heres the example i just mocked up.
    http://dev.wovenland.com/justaguy/

    PS. I'm not capturing any of the entered data. I promise.
  • Lemme know if that works. I will send you the code.
  • Yea, it's the php code that interests me atm. :)
  • Its not just your PHP code. your enctype (which ive never declared before in a form) is wrong apparently.

    		 <form action="send.php" method="post" ENCTYPE = "multipart/form-data" name="frmEmail" id="frmEmail">


    This line in your HTML form should have the "multipart/form-data" as the ENCTYPE
  • OK! Wow! I'm going to take it for a spin in a few shakes.

    Thanks, I'll report back ............
  • Hey dhechler

    Here's is how I modified it, more than likely it me, but I am getting a 404 error, and no email sent from site to my mailbox.

    http://pastebin.com/L8KnyCVF
  • Changes made include:

    copy/paste new enctype in form html

    $subject line string

    Sheaders .= 'From: My Site ' . "r/n/;

    and then everything below that, which is success page html

    Previously I was getting the success page in addition to a "no data capture e-mail"

  • I'll be home in 10. I'll look at it soon.
  • You forgot to close your PHP bracket
    ?>
    after
    mail($to, $subject, $message, $headers);
    //Send them back to the form page with a success message.
    // From here is the html to the success page after e-mail was $theResults = <<< EOD
  • I don't mean to highjack your time, get back to me as you are able ....

    I corrected the lack of closing php, no change. I guess there is only a finite number of ways I can toy with this, so as your able give it a second look.

    Thanks d
  • Sorry bout not getting back to ya sooner.

    Check this out. http://dev.wovenland.com/justaguy/

    thats your code working. lemme get the code i have for ya.


    <?
    $email = $_POST['email'];
    $questions = $_POST['myQuestions'];
    // subject
    $subject = 'Request for Information';

    // HTML message
    $message = '
    <html>
    <head>
    <title>' . $email . ' sent you a message.</title>
    </head>
    <body>
    <p style="font-size: 19px;">' . $email. '</p>
    <p style="font-size: 19px;"> '. $questions .'</p>
    </body>
    </html>
    ';

    // To send HTML mail, the Content-type header must be set
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= 'To: <' . $_POST['email'] . '>' . "\r\n";
    $headers .= 'From: My Web Site <justaguy@mywebsite.com>' . "\r\n";

    // Mail it
    mail($to, $subject, $message, $headers);
    //Send them back to the form page with a success message.
    // From here is the html to the success page after e-mail was $theResults = <<< EOD
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title>My Web Site/contact</title>
    <link href="main.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

    <div id="page-wrap" class="group">

    <div id="header">

    <div id="logo"></div>

    <h1>My Web Site</h1>

    <p>Yada yada</p>



    </div><!--END of header-->



    <div id="main-content">

    <div id="results">
    <p>Your request for additional information has successfully been forwarded.</p>
    <p>Thank you for your interest in My Web Site.</p>
    <p> You can expect a prompt response.</p>


    </div>


    </div><!--END of main-content-->

    <div id="sidebar">

    <div id="navigation">

    <ul>
    <li><a href="http://www.mysite.com/">Home<span></span></a></li&gt;
    <li><a href="http://www.mysite.com/about.html">About<span></span></a></li&gt;
    <li><a href="http://www.mysite.com/on-design.html">On Design<em>(my blog)</em><span></span></a></li>
    <li><a href="http://www.mysite.com/quote.html">Get A Quote<span></span></a></li>
    <li><a href="http://www.mysite.com/contact.html">Contact Me <span></span></a></li>
    </ul>

    </div><!--END of navigation-->

    </div><!--END of sidebar-->








    <div id=footer>

    <p>&copy; 2011 My Site / All Rights Reserved.</p>

    <div id="badge"></div>


    </div><!--END of footer-->

    </div><!--END of page-wrap-->

    </body>
    </html>
    <? EOD;
    echo "$theResults";

    ?>
  • The working code you posted does bring us to the "success" page, without an e-mail being sent. I have changed the opening php tag to include "php". I'm not certain what the string :

    $headers = 'MIME-Version: 1.0' . "\r\n";

    does, but I am by far behind you in knowledge.

    Are we certain that we needed to amend the ENCTYPE in the html? I'm just asking ..... again, I trust your knowledge over the lack of mine. I am just thinking about the changes that have been introduced and noting the deficiencies (now) compared to the code and symptoms we had in the beginning.

    "old code" ----> email sent, nothing captured, "success" page rendered vs

    "new code" ---> no email sent, uncertain if data is being captured, and failure of "success" page being rendered (I see that a "success" page is rendered here : http://dev.wovenland.com/justaguy/) but can't understand why it is failing on the site.

    It has to be something quite simple (and evasive) at this point.

    I appreciate you hanging in here with me (still)

  • Man, Im not sure why its not sending you an email on my test site. Heres how it all plays out for me.

    Fill out form
    http://i52.tinypic.com/2dhwuah.jpg

    Get Confirmation Page
    http://i56.tinypic.com/2j3407s.jpg

    Get Confirmation Email
    http://i51.tinypic.com/2i25tva.jpg

    The email comes almost immediately. Maybe it's getting dumped into your spam folder?
  • Here is something beneficial to add!

    I have been plugging an alternate (working) email address of mine into the form on the web page itself, and typing gibberish, then hitting the "send" button. As part of the script you've put together, the sender (from my web page) receives an e-mail to their e-mail address after completing the form.

    This is occurring, the "sender" will receive an email to their inbox and the question they type in the form is remitted to them. The "sender's" email address is formatted as a link that when clicked opens (in this case) a response through gmail.

    A little progress, huh? :-)
  • I guess I just restated what you already knew ..................
  • AHA! So in order for YOU to receive an email, just change this.


    // Additional headers
    $headers .= 'To: <' . $_POST['email'] . '>' . "\r\n";
    $headers .= 'From: My Web Site <justaguy@mywebsite.com>' . "\r\n";


    to this


    // Additional headers
    $headers .= 'To: <youremail@email.com>' . "\r\n";
    $headers .= 'From: My Web Site <justaguy@mywebsite.com>' . "\r\n";
    $headers .= 'Reply-To: <' . $_POST['email'] . '>' . "\r\n";


    This way you will receive the email. You can also send them a "Thank you" email with the previous code we were using.
  • CORRECTION. It should be this (sorry bout that)

    // Additional headers
    $headers .= 'To: youremail@email.com' . "\r\n";
    $headers .= 'From: My Web Site <justaguy@mywebsite.com>' . "\r\n";
    $headers .= 'Reply-To: <' . $_POST['email'] . '>' . "\r\n";


    Basically i removed the <> from the to email.
  • Before I try the above, this may be helpful, or not, in the form, on the website, I plug in my website e-mail address as "sender" and type gibberish, and no email sent to my web site email address.
  • let me copy all of the code over for you now. Just so we are both on the same page. Just to make sure.


    <?
    $email = $_POST['email'];
    $questions = $_POST['myQuestions'];
    // subject
    $subject = 'Request for Information';

    // HTML message
    $message = '
    <html>
    <head>
    <title>' . $email . ' sent you a message.</title>
    </head>
    <body>
    <p style="font-size: 19px;">' . $email. '</p>
    <p style="font-size: 19px;"> '. $questions .'</p>
    </body>
    </html>
    ';

    // To send HTML mail, the Content-type header must be set
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers. FILL IN YOUR EMAIL HERE
    $headers .= 'To: youremail@email.com' . "\r\n";
    $headers .= 'From: My Web Site <justaguy@mywebsite.com>' . "\r\n";
    $headers .= 'Reply-To: <' . $_POST['email'] . '>' . "\r\n";
    // Mail it
    mail($to, $subject, $message, $headers);
    //Send them back to the form page with a success message.
    // From here is the html to the success page after e-mail was $theResults = <<< EOD
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title>My Web Site/contact</title>
    <link href="main.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

    <div id="page-wrap" class="group">

    <div id="header">

    <div id="logo"></div>

    <h1>My Web Site</h1>

    <p>Yada yada</p>



    </div><!--END of header-->



    <div id="main-content">

    <div id="results">
    <p>Your request for additional information has successfully been forwarded.</p>
    <p>Thank you for your interest in My Web Site.</p>
    <p> You can expect a prompt response.</p>


    </div>


    </div><!--END of main-content-->

    <div id="sidebar">

    <div id="navigation">

    <ul>
    <li><a href="http://www.mysite.com/">Home<span></span></a></li&gt;
    <li><a href="http://www.mysite.com/about.html">About<span></span></a></li&gt;
    <li><a href="http://www.mysite.com/on-design.html">On Design<em>(my blog)</em><span></span></a></li>
    <li><a href="http://www.mysite.com/quote.html">Get A Quote<span></span></a></li>
    <li><a href="http://www.mysite.com/contact.html">Contact Me <span></span></a></li>
    </ul>

    </div><!--END of navigation-->

    </div><!--END of sidebar-->








    <div id=footer>

    <p>&copy; 2011 My Site / All Rights Reserved.</p>

    <div id="badge"></div>


    </div><!--END of footer-->

    </div><!--END of page-wrap-->

    </body>
    </html>
    <? EOD;
    echo "$theResults";

    ?>
  • Hold on, something seems amiss, my error no doubt.
  • What are you missing?
  • HTML code as well.

    <!DOCTYPE html>
    <html lang="">
    <head>
    <meta charset="utf-8">
    <title>justaguy form test</title>
    </head>
    <body>

    <form action="send.php" method="post" ENCTYPE = "multipart/form-data" name="frmEmail" id="frmEmail">
    <p><label for="email">E-mail:</label>
    <input name="email" type="text" class="text" id="email" tabindex="10" size="30" /></p>
    <p>
    <label for="myQuestions">My Questions:<br />
    </label>
    <textarea name="myQuestions" id="myQuestions" cols="45" rows="10" tabindex="20"></textarea>
    </p>
    <input type="submit" name="submit" id="submit" value="Send" tabindex="100" />
    </p>
    </form>
    </body>
    </html>
  • Geez! I tried the latest send.php and failed receiving any e-mails, after plugging in all my site/email info into place. Actually I tried a couple of times.

    Then I thought, well, I would go back and use the send.php that worked last, the one prior to the last one you submitted. No e-mail.! After making the changes to the "additional headers" and double checking my html.

    (what the hell, checkout my site here: nearnorthwebdesign.com

    stepping out for a smoke)



  • Well, in looking at your form code on http://www.nearnorthwebdesign.com/contact.html, you have this.

    <form action="contactFormprocess.php" method="post" enctype="text/plain" name="frmEmail" id="frmEmail">
    <p><label for="email">E-mail:</label>
    <input name="email" type="text" class="text" id="email" tabindex="10" size="30"></p>
    <p>
    <label for="myQuestions">My Questions:<br>
    </label>
    <textarea name="myQuestions" id="myQuestions" cols="45" rows="10" tabindex="20"></textarea>
    </p>
    <input type="submit" name="submit" id="submit" value="Send" tabindex="100">
    <p></p>
    </form>


    There are 2 problems i see with this form code.

    1. contactFormprocess.php does not exist on your server. I get a 404 page when i click submit.

    2. your enctype="text/plain" will not submit anything to your php file.

    Delete your enctype="text/plain". You dont need it at all.
    Create contactFormprocess.php on your server and insert the latest send.php code I'm including here.


    <?
    $email = $_POST['email'];
    $questions = $_POST['myQuestions'];
    // subject
    $subject = 'Request for Information';

    // HTML message
    $message = '
    <html>
    <head>
    <title>' . $email . ' sent you a message.</title>
    </head>
    <body>
    <p style="font-size: 19px;">' . $email. '</p>
    <p style="font-size: 19px;"> '. $questions .'</p>
    </body>
    </html>
    ';

    // To send HTML mail, the Content-type header must be set
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers. FILL IN YOUR EMAIL HERE
    $headers .= 'To: youremail@email.com' . "\r\n";
    $headers .= 'From: My Web Site <justaguy@mywebsite.com>' . "\r\n";
    $headers .= 'Reply-To: <' . $_POST['email'] . '>' . "\r\n";
    // Mail it
    mail($to, $subject, $message, $headers);
    //Send them back to the form page with a success message.
    // From here is the html to the success page after e-mail was $theResults = <<< EOD
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title>My Web Site/contact</title>
    <link href="main.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

    <div id="page-wrap" class="group">

    <div id="header">

    <div id="logo"></div>

    <h1>My Web Site</h1>

    <p>Yada yada</p>

    </div><!--END of header-->

    <div id="main-content">

    <div id="results">
    <p>Your request for additional information has successfully been forwarded.</p>
    <p>Thank you for your interest in My Web Site.</p>
    <p> You can expect a prompt response.</p>

    </div>

    </div><!--END of main-content-->

    <div id="sidebar">

    <div id="navigation">

    <ul>
    <li><a href="http://www.mysite.com/">Home<span></span></a></li&gt;
    <li><a href="http://www.mysite.com/about.html">About<span></span></a></li&gt;
    <li><a href="http://www.mysite.com/on-design.html">On Design<em>(my blog)</em><span></span></a></li>
    <li><a href="http://www.mysite.com/quote.html">Get A Quote<span></span></a></li>
    <li><a href="http://www.mysite.com/contact.html">Contact Me <span></span></a></li>
    </ul>

    </div><!--END of navigation-->

    </div><!--END of sidebar-->

    <div id=footer>

    <p>&copy; 2011 My Site / All Rights Reserved.</p>

    <div id="badge"></div>

    </div><!--END of footer-->

    </div><!--END of page-wrap-->

    </body>
    </html>
    <? EOD;
    echo "$theResults";

    ?>
  • <label for="myQuestions">My Questions:<br>
    </label>


    Really? I know it's not an important issue, but really it actually is. Your code needs to be logical and written properly, using BR tags for spacing is bad coding practise. You should only be using CSS for styling!
  • @Brightonmike Yeah. I didnt make that, but BR tags are allowed to be used. I would have done it after the closing label tab, but I do definitely think you could use CSS to do this instead.
  • 1. contactFormprocess.php does not exist on your server. I get a 404 page when i click submit. "quoteFormprocess.php" exists because that is my (current, soon to be replaced) attempt at processing the quote form on my quote page.

    Response: contactFormprocess.php isn't being referenced to in my html any longer. That was the name/file for my "old" .php processing. Since working with you I am now simply adopting the send.php name/file to keep from getting (too) confused. I don't see that it is found on the server atm (?)

    2. your enctype="text/plain" will not submit anything to your php file.

    Response: The enctype for the send.php is "multipart/form-data". I just corrected the "case" of the text to lower case, it was all caps just now.

    I don't understand, the issues you are raising here I can't see on the server, unless the number of changes that we've been going through are not being promptly updated on the server, but that's not making sense if you are "seeing" it and I am not (?)
  • The
    tags aren't necessary, I agree, that was just a carry over from the tutorial.
  • Just to be clear. I am talking about http://www.nearnorthwebdesign.com/contact.html

    The form on this page is using this code below.


    <form action="contactFormprocess.php" method="post" enctype="text/plain" name="frmEmail" id="frmEmail">
    <p><label for="email">E-mail:</label>
    <input name="email" type="text" class="text" id="email" tabindex="10" size="30" /></p>
    <p>
    <label for="myQuestions">My Questions:<br />
    </label>
    <textarea name="myQuestions" id="myQuestions" cols="45" rows="10" tabindex="20"></textarea>
    </p>
    <input type="submit" name="submit" id="submit" value="Send" tabindex="100" />
    </p>
    </form>


    Is that the page that you have been updating?