PHP form submission to email and MySQL?

I have a form that works to either submit data to a MySQL database and then echo an appropriate response to the user OR a version that sends the data to someone who will respond to their inquiry. What I want is:

[ol][li]User fills in form[/li]
[li]User sees response on same web page that echoes firts and last name[/li]
[li]Form contents sent via email to a particular email address[/li]
[li]Form is submitted to MySQL database[/li][/ol]

Yahoo is the host, so using their email form submission, it will send and redirect them to another page that I may specify. If it is not specified, the page automatically redirects to show what the user submitted, which I do not want.

The MySQL version works and echoes the name, but then that would require constant monitoring of the DB, which is not going to happen.

Here is the code…

[php]

<?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO contact (firstname, lastname, emailAddress, phoneNumber, stAddress, aptNumber, city, fiftystates, zipCode, `time`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['firstname'], "text"), GetSQLValueString($_POST['lastname'], "text"), GetSQLValueString($_POST['emailAddress'], "text"), GetSQLValueString($_POST['phoneNumber'], "text"), GetSQLValueString($_POST['stAddress'], "text"), GetSQLValueString($_POST['aptNumber'], "text"), GetSQLValueString($_POST['city'], "text"), GetSQLValueString($_POST['fiftyStates'], "text"), GetSQLValueString($_POST['zipCode'], "text"), GetSQLValueString($_POST['time'], "text")); mysql_select_db($database_**************, $****************); $Result1 = mysql_query($insertSQL, $**********) or die(mysql_error()); } ?>

[/php]

[code]<form method=“POST” id=“form1” name=“form1” action="<?php echo $editFormAction; ?>“post”>

First Name* (Please, let me know who I am contacting)
Last Name
Email Address* A value is required.Invalid format.
Phone Number* Example: (000) 000-0000
Street Address
Suite | Apt | Unit
City
State Select a State Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware Florida Georgia Hawaii Idaho Illinois Indiana Iowa Kansas Kentucky Louisiana Maine Maryland Massachusetts Michigan Minnesota Mississippi Missouri Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota Ohio Oklahoma Oregon Pennsylvania Rhode Island South Carolina South Dakota Tennessee Texas Utah Vermont Virginia Washington West Virginia Wisconsin Wyoming
Zip Code* Invalid format.
When is the best time to reach you?
 
[/code] This is the version that submits to the DB. What do I need to do to make it email the same contents?

Depending on your host, the PHP mail() function might be enabled. If it isn’t, you’ll need to connect to a SMTP server. If it is, you’re in luck.

Add a mail() call just after your mysql_query call. The mail() function takes as parameters the following:

  • To
  • Subject
  • Content
  • Headers
    From there, you can easily format your message to fit something you want.

More info can be found here: http://php.net/manual/de/function.mail.php

Thanks for your help- I do not think they allow mail(). http://help.yahoo.com/l/us/yahoo/smallbusiness/webhosting/php/php-33.html

I will have to try to do it with smtp, or figure something else out. I have never done smtp, but from what I can tell, it may require modifying the php.ini file. If that is the case, they do not allow that, either.

A possible solution for you is https://github.com/Synchro/PHPMailer . This can handle SMTP servers.

Thanks for posting that to github- I was going to try that last night, but I ended up talking to a guy at Yahoo! that gave me a sample script for php (after 56 minutes on the line).

This is what he gave me[php]

Untitled <? $email = 'xxxxxxxxxxxxxxx'; $subject = 'Yahoo! Customer Care Mail Function Test'; $message = 'This message is a test to make sure your mail forms are working OK.'; $headers = "MIME-Version: 1.0\r\n"; $headers.= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers.= "X-Nyar-Nyar: Test header to test with\r\n"; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) { echo "Invalid email address!"; } elseif ($subject == "") { echo "No subject!"; } elseif (mail($email,$subject,$message,$headers)) { echo "Success! Whatever else doesn't work, it ain't PHP. This mail has been sent to the MTA"; } else { echo "Hold on: email to $email has failed."; } ?>

[/php]

After inserting it before the mysql code, it send an email with the text from the code and it ALSO posted to the database. How do I reference the text the user inputs? I need to echo the form data or use variables?

So the mail function was enabled. Looks like it’s the easiest way for everyone!

Yes, it is enabled! Thank goodness. The only thing is I can’t get it to echo what is in the form… I don’t want anyone to write the code for me, but do you have any idea how to get it to post the info to the email, too?

Sorry about the delay in getting to the bottom of your question. In the code given by the Yahoo! guy, this line represents the email message:
[php]$message = ‘This message is a test to make sure your mail forms are working OK.’;[/php]

Assuming you’re still in the scope of your previous script, you should have access to the $_POST superglobal array, which contains all the info submitted by the user. Format a string as you see fit. If it doesn’t work, paste it here and I’ll fix it :slight_smile:

Looks like we replied to each other at the exact same time.

You’ll need to do a second mail call if you’re on PHP4.x or 5.0. If you’re on anything beyond that, you should be able to do this:
[php]elseif (mail($email.";[email protected]",$subject,$message,$headers)) {[/php]

This depends on your host.

So- inside the $message, it would look like this?
[php] $message = ‘($_POST[‘firstname’], “text”),
($_POST[‘lastname’], “text”),
($_POST[‘emailAddress’], “text”),
($_POST[‘phoneNumber’], “text”),
($_POST[‘stAddress’], “text”),
($_POST[‘aptNumber’], “text”),
($_POST[‘city’], “text”),
($_POST[‘fiftyStates’], “text”),
($_POST[‘zipCode’], “text”),
($_POST[‘time’], “text”));’[/php]

This will throw you tons of errors. Consider this:
[php]$message = “First name: {$_POST[‘firstname’]}
Last name: {$_POST[‘lastname’]}
Email: {$_POST[‘emailAddress’]}
Phone: {$_POST[‘phoneNumber’]}
Address: {$_POST[‘stAddress’]}
{$_POST[‘aptNumber’]}
{$_POST[‘city’]}
{$_POST[‘fiftyStates’]}
{$_POST[‘zipCode’]},
At: {$_POST[‘time’]}”;[/php]

This will allow you to also have a slightly nicer format for all your data. The {} encapsulation tells PHP what to expect. I changed the outer quote to a double quote to prevent PHP from breaking due to the single quotes in your array keys.

Ahhh… awesome. I will give it a try, right now. If you are ever in the North AL area, let me know and I will buy you a beer!

Will do, though it’ll have to be a coke, and it might not be for a while. Alcohol allergy, and I’m only just settling in DC - and I left my motorbike in France :-X

Ouch- alcohol allergy. I don’t want to know how you learned of that!

2 things that have come up after I get the email part to work… the text for both the drop down menu and for the part where it echoes back the first and last name are all TINY- basically a couple of px per letter. It may be my CSS breaking up from renaming divs during the process. I probably just need to work on the links. Also, for some reason it is sending out 2 emails- BUT, I think that is because of a change I made that hasn’t been fully deleted on Yahoo!.

I greatly appreciate your help! What part of France are you from?

A bad prank at a school party that ended…funnily.

2 things that have come up after I get the email part to work... the text for both the drop down menu and for the part where it echoes back the first and last name are all TINY- basically a couple of px per letter. It may be my CSS breaking up from renaming divs during the process. I probably just need to work on the links. Also, for some reason it is sending out 2 emails- BUT, I think that is because of a change I made that hasn't been fully deleted on Yahoo!.

Can’t help you on that - both are outside of any PHP possible work. For the drop-down, it’s most likely CSS. For the double mail - ask yahoo? It’s not such a big deal, though.

I greatly appreciate your help! What part of France are you from?

The general Nancy area (Vosges. It’s the département directly west of Alsace), though I have not lived there much at all. I’ve spent most of my life in Belgium and the UK. Whereabouts in AL are you? Hope the weather is a bit better over there :stuck_out_tongue:

The general Nancy area (Vosges. It's the département directly west of Alsace), though I have not lived there much at all. I've spent most of my life in Belgium and the UK. Whereabouts in AL are you? Hope the weather is a bit better over there :p

Have recently done research concerning the Alsace Lorraine region- pretty interesting how many times it changed hands between France and Germany over the years. Especially bad that you have an alcohol allergy, considering it is a brewing region!

Belgian chocolate is absolutely the best (callebeaut).

I live in the Shoals area. Ever heard Sweet Home Alabama? There is a line in it ‘Muscle Shoals has got the swampers’. They are talking about a group of studio musicians- I know one of them. Very talented musicians who have recorded a LOT of hit songs. Also, the birthplace of Helen Keller.

The history of this region is more than just swapping hands, though. It did so mostly because of wars, which led to the construction of a few lines of defense around here. My grandmother lives in Remiremont, and this was literally the border between France and Germany after the Franco-Prussian war. There are forts on the mountains left over from that time.

Quick run-down of it:

  • 1870: France gets absolutely gobsmacked in under 2 weeks by Germany on a war that Napoléon III started (the history of it is rather amusing). Germany takes Alsace-Lorraine and the German emperor is crowned in Paris
  • 1918: France gobsmacks Germany, takes Alsace-Lorraine back and demands of Germany to pay extremely high tolls as revenge for 1870
  • 1937: Hitler gobsmacks France, takes Alsace-Lorraine
  • 1945: the allies gobsmack Germany, take back Alsace-Lorraine

In addition to this, there are two lines of forts around the border - one built in 1890, the other build in 1930-ish. The reason for them was because the allies thought that Germany would try it from there… Twice, they went through Belgium. :stuck_out_tongue:

You guys in states do not actually know what real Belgian choc is. It’s sad to say, but it’s like a lot of stuff from Europe - never actually gets to there in its unrefined (= unpolluted) form. The same goes for a lot of stuff.
If you think belgian choc is super sweet, you’re wrong :smiley:

I have. I did not know that :o

cool- thanks for the information on the history of the region.

This is a link to the Muscle Shoals movie that premiered at the Sundance Film Festival, recently. It is an awesome history of the music industry here. It includes the “Swampers” history and FAME studio owner. Lots of people recorded and still record here. http://muscleshoalsmovie.com/

Sponsor our Newsletter | Privacy Policy | Terms of Service