Hi everyone,
I was wondering if someone out there would be nice enough to help me with my script. I recently scripted a contact form that has to validate certain fields before being submitted to a database. The strangest thing is, that even if it’s not filled out, could be any field, it will still go through without giving the message of a missing field.
If anyone out there could help, it would be grand. Thank you.
[code=php]<?php
$to = “[email protected]”;
$from = “from: ” .stripslashes($_POST[‘name’]) .”<” .$_POST[’email’] .”>”;
$subject = “I want to join”;
$message = stripslashes($_POST[‘name’]) . “n” . $_POST[’email’] . “n” . $_POST[‘country’];
mail($to, $subject, $message, $from);?>
<?php
//Validate form
if (isset($_POST[‘submit’])) {
$message = NULL;
//Validate name field
if (empty($_POST[‘name’])) {
$n = FALSE;
$message .='<p>You didn’t enter your name!</p>’;
} else {
$n = $_POST[‘name’];
}
//Validate e-mail field
if (empty($_POST[’email’])) {
$em = FALSE;
$message .='<p>You didn’t enter your e-mail!</p>’;
} else {
$em = $_POST[’email’];
}
//Validate country field
if (empty($_POST[‘country’])) {
$c = FALSE;
$message .='<p>You didn’t enter your country</p>’;
} else {
$c = $_POST[‘country’];
}
//Validate comments field
if (empty($_POST[‘country’])) {
$co = FALSE;
$message .='<p>You didn’t make any comments!</p>’;
} else {
$co = $_POST[‘comments’];
}
//If everything is filled in
if ($n && $em && $c && $co) {
//Add fan to the database
define (‘db_user’, ‘xxx_xxx’);
define (‘db_password’, ‘xxx);
define (‘db_host’, ‘xxx’);
define (‘db_name’, ‘xxx_xxxx’);
$dbc = @mysql_pconnect(db_host, db_user, db_password) OR die(‘Could not connect to MySQL: ‘ . mysql_error());
@mysql_select_db (db_name) OR die(‘could not connect to the database: ‘ . mysql_error());
$query = “INSERT INTO fans (name, email, country, comments) VALUES (‘$n’, ’em’, ‘$c’, ‘$co’)”;
$result = @mysql_query ($query);
if ($result) {
echo ‘<p>Thank you for joining!</p>’;
exit();
} else {
$message .= ‘<p>We are sorry, but you could not be added to the fanlisting due to a system error. We apologize for any inconvenience, please try again later.</p><p>’ . mysql_error() . ‘</p>’;
}
mysql_close();
} else {
$message .= ‘<p>We are sorry. Please try again later</p>’;
}
}
?>