PDA

Click to See Complete Forum and Search --> : Sending Form to Email and MySQL


Olly79
Feb 3rd, 2009, 03:24 PM
Hi all,

Just wondering if someone can help me with the following - I would like to send my web form to not only email but to a MySQL DB. I have the following code for all of the above and now need help on how to bring it all together to achieve what I have outlined above.

Web Form:
<div>
<form name="contact" method="post" action="contactprocess.php">
<strong>Name:</strong><br/>
<input type="text" name="ename" size="30"><br/>
<strong>Email:</strong><br/>

<input type="text" name="eemail" size="30"><br/>
<strong>Subject:</strong><br/>
<input type="text" name="esubject" size="30"><br/>
<strong>Message:</strong><br/>

<textarea name="emessage" cols="30" rows="10"></textarea><br/>
<input type="submit" name="esubmit" value="Send Mail" style="cursor:pointer">
<input type="hidden" name="eip" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>">
</form>
</div>

PHP Code to Send Form:
<?php

//Some variables
$mymail = "myemail@address.com";
$ename = $_POST['ename'];
$eemail = $_POST['eemail'];
$esubject = $_POST['esubject'];
$emessage = $_POST['emessage'];
$eip = $_POST['eip'];

//Function to check email address
function checkemail($eemail) {
if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$eemail)) {
return true;
}
else {
return false;
}
}

//Mail Processing
if ($_POST['esubmit']) {
//Check for blank fields
if ($ename == "" || $eemail == "" || $esubject == "" || $emessage == "") {
echo "<p>It appears that you left a blank field.<br/> Please make sure you fill everything in.</p>";
}
//Check to see if the email address is valid
else if (checkemail($eemail) == false) {
echo "<p>It appears that you enter an invalid email address.<br/> Please check your email again.</p>";
}
//Send the email if there's no error
else {
$body = "$emessage\n\nName: $ename\nEmail: $eemail\nIp: $eip";
mail($mymail,$esubject,$body,"From: $eemail\n");
echo "<p>Thank you for your email $ename!</p>";
}
}

?>

Possible PHP code to send Form to DB:

<?
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
mysql_query("INSERT INTO `data` VALUES ('$name', '$email','subject','$message')");
Print "Your information has been successfully added to the database.";
?>

Can someone please help me bring the form and DB script together based on the above (an example based on the code used would be great).

Many thanks

kows
Feb 4th, 2009, 12:59 AM
just add the mysql code to the form? all I did here was copy and paste the second bit of code to the first part, in the same place that you were calling the mail() function. of course, you should re-use the original variables ($ename, $eemail, etc) instead of redefining new ones that have the same values. }
//Send the email if there's no error
else {
$body = "$emessage\n\nName: $ename\nEmail: $eemail\nIp: $eip";
mail($mymail,$esubject,$body,"From: $eemail\n");
echo "<p>Thank you for your email $ename!</p>";

$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
mysql_query("INSERT INTO `data` VALUES ('$name', '$email','subject','$message')");

}
}you should really look up some PHP tutorials on basic if/else statement syntax, and start trying to build something from scratch. this was something that seems unbelievably simple to me.

ngreenwood6
Feb 4th, 2009, 08:15 AM
umm not too sure but doesn't the query actually need the values that you are going to input:


mysql_query("INSERT INTO data(name,email,subject,message) VALUES ('$name', '$email','subject','$message')");


I have never seen a query without it.

kfcSmitty
Feb 4th, 2009, 09:19 AM
umm not too sure but doesn't the query actually need the values that you are going to input:


mysql_query("INSERT INTO data(name,email,subject,message) VALUES ('$name', '$email','subject','$message')");


I have never seen a query without it.


If you're not inserting into all fields in the table, then you need to list the fields you wish to input as you have done. If name, email, subject and message are the only fields in the table, you do not need to specify that you're inserting into all of them.

Not sure if this is true for all DBMS, but I know it is the case for MySQL, and I believe it is the case for MS SQL as well.

manavo11
Feb 5th, 2009, 06:02 PM
You also have to have the correct order of the fields. If you specify them you just need to match them 1 for 1.