Click to See Complete Forum and Search --> : Can i both insert form data in an sql db AND email the contents at the same time?


nil
07-20-2003, 10:09 AM
Hello and thank you for providing a great forum. I have an issue re: submitting a form that i need help with.

I have an HTML form that currently writes to a MySQL db using when the user hits 'Submit'. The PHP code looks like so:

if (isset($_REQUEST['submit']))
{
mysql_connect('localhost','username','userpass');
mysql_select_db('db_name');
mysql_query("INSERT INTO table_name(field1, field2) VALUES ('" .
$_REQUEST['firstname'] ."','" . $_REQUEST['lastname'] . "')");

this functionality works fine. is there a way to also email the form to several people when the user 'Submits'?

Any help is aprecciated. Thanks in advance.

tobyw_1969
07-20-2003, 11:03 AM
You can e-mail the information using the mail() command.

If all the data you need is already 'in' the PHP page, then its easy - would look something like this...


mail($emailto, $subject, $mailmessage, $headers);

or if you want just to send the same message every time, you could include the relevant stuff directly like this

mail("harrypotter@hogwarts.com","New Magi-viagra","Use our new product and never have your wand let you down again!");


The headers part is optional, but if you wanted to make your message HTML then you would need to include some headers in there. For more on that, there is a post a couple of weeks back which will tell you about that - because I had a question on it and got some really useful replies.

If you need to get data out of the database to send in your e-mail, you would need to do a bit more. Like, say you are storing data with a unique ID which is auto-incremented...and you want to quote that ID number in the mai - after you add the new data, you would then need to re-interrogate the DB to get the id of the entry you just created. Like this

$result = mysql_query("SELECT max(id_num) FROM my_database");

$row=mysql_fetch_row($result);

$the_id_num= $row[0];


Then you can include $the_id_num in your message too. Etc..


Hope that made sense... good luck.

nil
07-20-2003, 12:10 PM
Thanks for the quick response. I don't know why i didn't think of this :D