Here is the php code that I have in the <head> of my contact page.
- Code: Select all
<?php
$hostUrl="myhostname";
$userName="my-db-username";
$password="my-password";
$connectID = mysql_connect($hostUrl, $userName, $password)
or die ("Sorry, can't connect to database");
mysql_select_db("my-db-name", $connectID)
or die ("Unable to select database");
mysql_close($connectID);
include ("_include-Functions/form_validation.php");
if (@$_POST['submitted']) {
$first_name = @$_POST['first_name'];
$last_name = @$_POST['last_name'];
$email = @$_POST['email'];
$msg = @$_POST['message'];
// if magic quotes on, remove Magic Quotes effect
if ( get_magic_quotes_gpc() ) { // no arg needed - returns 1 if magic quotes are on, else 0
$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$email = stripslashes($email);
$msg = stripslashes($msg);
}
$error_msg=array();
$valid = verifyAlphaNum ($first_name);
if (!$valid){
$error_msg[]="First Name can only contain letters.";
}
$valid = verifyAlphaNum ($last_name);
if (!$valid){
$error_msg[]="Last Name can only contain letters.";
}
$valid = verifyEmail ($email);
if (!$valid){
$error_msg[]="Email must be a valid format (i.e. jimihendrix@gmail.com). Check it and try again.";
}
$valid = verifyText ($msg);
if (!$valid){
$error_msg[]="Message can only contain letters, numbers and basic punctuation \" ' - ? ! ";
}
$destination_email="my-email-address";
$email_subject="Contact from my-website.com";
$email_body = "$first_name\n$last_name\n$msg\n$email";
$headers = "From: $_POST[email]\r\n";
if (!$error_msg) {
mail ($destination_email, $email_subject, $email_body, $headers);
header ('Location: thankyou.php');
exit();
}
}
?>
...AND the form's code... within the HTML
- Code: Select all
<div id="form">
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<?php
if ($error_msg) {
echo "<ul>\n";
foreach ($error_msg as $err) {
echo "<li>".$err."</li>\n";
}
echo "</ul>\n";
}
?>
<label for="first_name" >First Name*</label><br />
<input name="first_name" type="text" size="20" id="first_name" value="<?php echo $first_name ?>" /><br />
<label for="last_name">Last Name*</label><br />
<input name="last_name" type="text" size="20" id="last_name" value="<?php echo $last_name ?>" /><br />
<label for="email" >Email:</label><br />
<input name="email" type="text" value="<?php echo $email ?>" /><br />
<label for="message">Message:</label><br />
<textarea name="message" rows="8" cols="30" id="form-input"><?php echo $msg ?></textarea><br />
<input class="input" name="submitted" type="submit" value="Submit" />
</form>
<!--end form--></div>
The only problem is that it is not storing my contacts from the form in my database, as I hoped that it would.
Wondering if you, or someone here, wouldn't mind critiquing what I have going here for my contact form.
And if someone can tell from this why it is not being stored in the database, that would be incredible, but I don't know if you guys prefer to not have database questions here on the forum.