Jump to content

Contact form - checking against database before sending


erme

Recommended Posts

Hi,

 

I've pulled a few bits of code I've found together to create the below form. I am trying to get it to check what's entered in the 'Name' field against a database field called 'dbName'. If the name enetered already exists it asks the user to confirm by pressing another <button>, else it just sends the emails.

 

<?php

if (isset($_POST['submit'])) {

$Name = trim(stripslashes($_POST['Name']));

$clientTo="[email protected]";

$message_enq="";
$message_enq.="Name: $Name \n";


mail($clientTo, $subject, $message.$message_enq.$messagefooter, $emailheaders);



$query = "SELECT dbName FROM Tablename WHERE dbName='{$_POST['Name']}'";
$resource = mysql_query($query);
  
if (empty($Name)) {
	print "<h1>Thank you. Email sent!</p>";
	exit;
}
else {

	if (mysql_num_rows($resource) == 0){
    		
	}
	else {
		print "Are you sure you are not already listed?<br /><br />";
		echo "<button type=\"submit\" name=\"submit\" id=\"submit\">No I'm Not Listed. Add Me</button>";
		exit;
    		}
}

}

?>

<form id="contactBox" method="post" action="page.php">
<input type="text" id="Name" name="Name" value="<?=$Name;?>" /><br />
<button type="submit" name="submit" id="submit">Add Me</button>
</form>

 

Thanks for looking.

 

 

If nothing is entered the 'Email sent' message is displayed and email sent (correct)

If a name is entered that isn't in database an email is sent (correct) but 'Email sent' message is not displayed.

If a name is entered that is in database the 'Are you sure you are not already listed' message is displayed but email is sent (don't want this) and the button doesn't do anything.

This is being sent to anyone who submits:

mail($clientTo, $subject, $message.$message_enq.$messagefooter, $emailheaders);

You need to put it inside the condition like:

	

if (empty($Name)) {
mail($clientTo, $subject, $message.$message_enq.$messagefooter, $emailheaders);

print "<h1>Thank you. Email sent!</p>";
exit;
}
else {
if (mysql_num_rows($resource) == 0){
    
mail($clientTo, $subject, $message.$message_enq.$messagefooter, $emailheaders);

}

 

You also need to add an onclick to your button so it does something

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.