|
-
Feb 3rd, 2009, 04:24 PM
#1
Thread Starter
Hyperactive Member
Sending Form to Email and MySQL
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:
HTML Code:
<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 Code:
<?php
//Some variables
$mymail = "[email protected]";
$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:
PHP Code:
<?
$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
-
Feb 4th, 2009, 01:59 AM
#2
Re: Sending Form to Email and MySQL
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.
PHP Code:
} //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.
-
Feb 4th, 2009, 09:15 AM
#3
Hyperactive Member
Re: Sending Form to Email and MySQL
umm not too sure but doesn't the query actually need the values that you are going to input:
Code:
mysql_query("INSERT INTO data(name,email,subject,message) VALUES ('$name', '$email','subject','$message')");
I have never seen a query without it.
If I helped you please rate me.
-
Feb 4th, 2009, 10:19 AM
#4
Re: Sending Form to Email and MySQL
 Originally Posted by ngreenwood6
umm not too sure but doesn't the query actually need the values that you are going to input:
Code:
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.
-
Feb 5th, 2009, 07:02 PM
#5
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|