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@email.co.uk";

$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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.