Im my form to send emails I have a button called "Save".When I click the button I want to send the email as well as insert data into my database(values of sender,receiver).I can send th email but i dont know how to insert the data into the database at the sametime.Can anyone help?

Recommended Answers

All 9 Replies

Im my form to send emails I have a button called "Save".When I click the button I want to send the email as well as insert data into my database(values of sender,receiver).I can send th email but i dont know how to insert the data into the database at the sametime.Can anyone help?

depending on which database you use, you would do a simple sql insert.

the example below is for MySQL

<?php
$con = mysql_connect("localhost","database username","database password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");

mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')");

mysql_close($con);
?>

No Iwant to execute the code which inserts data into the database when I click the "Send" button.How can I do it?

No Iwant to execute the code which inserts data into the database when I click the "Send" button.How can I do it?

You will need to be clearer on what you are doing, because I answered a valid response based off of your question.

When you click the send button, the usual way is you call another script via get or post. alternatively you can call an ajax so the page doesnt "reload". Since you didn't state which you are doing, I'll assume the first case.

If that's the case, where you are doing your emailing, you would do a sql insert just like what I already posted, but with your data

This is where my emailing is done.
<Form enctype="multipart/form-data"
action="http://abc.net/sendmail.php
method="post" name="frm1"
onsubmit="javascript:return myValidate();" >
.
.(some code)
.
<input type="submit" name="Submit" value="Send" />
Can you tell me where should I execute my insert code into database

This is where my emailing is done.
<Form enctype="multipart/form-data"
action="http://abc.net/sendmail.php
method="post" name="frm1"
onsubmit="javascript:return myValidate();" >
.
.(some code)
.
<input type="submit" name="Submit" value="Send" />
Can you tell me where should I execute my insert code into database

Like i said, you need to insert the code in my first response.

It would go in sendmail.php

Is there a way to insert data into the database when the "Send" button is clicked?If so can you tell me how?Please show me the place where I have to put the INSERT INTO statement

Is there a way to insert data into the database when the "Send" button is clicked?If so can you tell me how?Please show me the place where I have to put the INSERT INTO statement

Like I said, the code (which I previously posted) goes in sendmail.php (like I previously posted).

But my sendmail.php is hosted in the web therefore i think i cant insert data to my database from there.Any idea how to do this?

I'd be surprised to hear that you have access to the HTML but not the PHP.

but if that's the case, your only option would be to make an ajax call to another php script that does the insert, then let the rest work like it already does.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.