Trouble with simple HTML form and PHP script
September 6, 2007 12:02 PM   Subscribe

I need to know why my HTML form isn't sending me e-mail when the user submits it. It also doesn't go to the header page after submission. However, the submitted info DOES show up in the URL. ( e.g., http://www.example.com/contactus.html?Name=John&Title=CEO....)

I have a simple form that's supposed to send me an e-mail with the user's submissions. What am I doing wrong with my HTML or PHP code? All I did was modify the code from this site. (When I tested their actual code, verbatim, it does work.)

Please click here for the code in question.

[This question is being posted for desjardins', so any in-thread replies will come from her.]
posted by salvia to Computers & Internet (18 answers total)
 
I'm not sure if the code got copied and pasted directly from where you're working, but I found several typos that could be affecting it. For example, in the very first line there are two (an extra space and an extra quote):

[form action=" contactus.php" method=""post"]

That may not be causing your problem, but it won't be helping either.
posted by Kimberly at 12:12 PM on September 6, 2007


First step is validate your HTML.
posted by cmiller at 12:22 PM on September 6, 2007


I can never remember which languages are case sensitive but I'd try matching the cases on all your variables.
posted by TwoWordReview at 12:30 PM on September 6, 2007


Google tells me that PHP is in fact case sensitive so that's probably your issue.
posted by TwoWordReview at 12:32 PM on September 6, 2007


I don't see any case issues in the posted code.

Kimberly is probably right - the space before the filename is undoubtedly causing a problem, the extra quote probably wont be an issues since you're using $_REQUEST rather than $_POST but you should fix it anyway.

It might help us to know what exactly is happening when you submit the form.
posted by missmagenta at 12:37 PM on September 6, 2007


Just to add, the extra quote is the reason the data is showing in the URL - essentially you've put action = "" - so it is defaulting to a $_GET request
posted by missmagenta at 12:39 PM on September 6, 2007


I'd start with fixing that extra quote. Then make sure your server is running PHP >= 4.1.0, since earlier versions do not have a $_REQUEST variable.
posted by sonofslim at 12:40 PM on September 6, 2007


I'm not sure what's going on with that mail() statement in the PHP, but it looks like you're trying to pass all of the form data to the mail() command via separate parameters without concatenating them into a coherent email body first:

mail( "me@REDACTED.com", "Contact Form Message", $name, $title, $company, $address, $city, $state, $zip, $country, $phone, $industry, $message, "From: $email" );

PHP's mail() command only takes a few parameters, three of which are required:
  1. The "To:" address;
  2. The subject of the email; and
  3. The body of the email.
What you probably want (in addition to the HTML fixes Kimberly mentioned above) is something more like:

mail( "me@REDACTED.com", "Contact Form Message", "You got a message from $name, $title with $company at $address, $city, $state, $zip, $country. Their phone number is $phone, and they're in the $industry. The message is $message", "From: $email" );

And for the love of all that's holy, please secure that mail script (I'm not qualified to tell you how, but Googling "secure PHP mail script" should give you some good leads). Spammers will have that thing cranking out spam by the thousands within seconds of finding it.
posted by Doofus Magoo at 12:41 PM on September 6, 2007


Yeah, please don't use that script without at least applying strict validation to $email, otherwise it's wide open to email injection.
posted by malevolent at 12:52 PM on September 6, 2007


Wow, thanks for all the answers. I will check these out and report back.

Special thanks to salvia for posting this for me!
posted by desjardins at 12:52 PM on September 6, 2007


Something's still funky. There is no space in form action="contactus.php" - Google Docs somehow inserted that. Removing the double quote didn't help.

What I get upon submitting the form is that the URL changes to the $_GET request. That's all. No "thank you for your information" page, no e-mail coming to me.

Your security concerns are duly noted and will be done ASAP.
posted by desjardins at 1:18 PM on September 6, 2007


1. Don't use that form. PHPMailer is the go-to for this sort of thing.
2. Use pastebin.com for pasting code (you'll have to remove stuff like emails yourself, however).
3. The 'print' statement is *very* useful for debugging. If you add 'print 'hey';' to your script, you'll at least know if you're hitting the right page. Other good debug statements are 'print_r', which prints arrays and objects, and 'var_export', which can dump to a string.
If you're not getting redirected by the location header, it sounds like you're not getting redirected to the right place. Additionally, it's a good habit to use the FULL php open tag (<?php) rather than (<?) as often Windows machines won't process scripts with the short opener, giving you a blank page.
posted by ragaskar at 1:44 PM on September 6, 2007


sorry, that should read "not posting to the right place".
posted by ragaskar at 1:45 PM on September 6, 2007


Getting closer - I got the e-mail portion to work, sort of. I changed the code to Doofus Magoo's suggestion, and this is the e-mail I received:

You got a message from , with at , , IDIdaho, , Taiwan. Their phone number is 424-123-1123, and they're in the Aerospace. The message is testing.
posted by desjardins at 1:46 PM on September 6, 2007


Address the case inconsistencies -- e.g., the "name" field is proper case (<input name="Name">) in the form, but all lower case in the PHP ($_REQUEST['name']).
posted by Doofus Magoo at 1:48 PM on September 6, 2007


Yep, that was why the e-mail wasn't formatting properly. I caught that just after I posted. Now my last issue is why the header isn't working. Here's what I get (added br tags to avoid scrolling):
Warning: Cannot modify header information - headers already sent by (output started at /home/mydomain/public_html/contactus.php:1) in /home/mydomain/public_html/contactus.php on line 18

posted by desjardins at 1:57 PM on September 6, 2007


You can't use the header() function after any output is sent to the user's browser. If you're sure that contactus.php doesn't explicitly print/echo anything prior to calling the header() function, make sure there's not a blank line or space, or anything else up around, oh say, line 1 of contactus.php (probably preceding the <?PHP tag) that's inadvertently being output.
posted by Doofus Magoo at 2:02 PM on September 6, 2007


you can also use an output buffer to send headers arbitrarily.

http://us.php.net/manual/en/ref.outcontrol.php
posted by sonofslim at 2:59 PM on September 6, 2007


« Older What are the most effective qualifications and...   |   What is the name of this children's book? Newer »
This thread is closed to new comments.