/    Sign up×
Community /Pin to ProfileBookmark

Can i both insert form data in an sql db AND email the contents at the same time?

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.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@tobyw_1969Jul 20.2003 — 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("[email protected]","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.
Copy linkTweet thisAlerts:
@nilauthorJul 20.2003 — Thanks for the quick response. I don't know why i didn't think of this ?
×

Success!

Help @nil spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...