Jump to content

PHP - Contact Form - Receiving blank emails


splinter_1234

Recommended Posts

I have pretty basic PHP knowledge. I have set up a contact form which works well, however I seem to be receiving blank emails.

 

I used dreamweaver to perform validation on the submit button so I don't understand how this is happening.

 

Any help would be much appreciated.

 

form.html code -

 

<form id="form1" name="form1" method="post" action="mailer.php">

<label>Name: </label>

<input name="Name" type="text" id="name" size="20" />

<label>Phone: </label>

<input name="Phone" type="text" id="phone" size="20" />

<label>Best time to call: </label>

<input name="Time" type="text" id="time" size="20" />

<input name="submit" type="submit" id="submit" onclick="MM_validateForm('name','','R','phone','','R','time','','R');return document.MM_returnValue" value="Submit" />

</form>

 

 

mailer.php code -

 

<?php

 

// get posted data into local variables

$EmailFrom = "";

$EmailTo = "";

$Subject = "";

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

$Phone = Trim(stripslashes($_POST['Phone']));

$Time = Trim(stripslashes($_POST['Time']));

 

// prepare email body text

$Body = "";

$Body .= "Name: ";

$Body .= $Name;

$Body .= "\n";

$Body .= "Phone: ";

$Body .= $Phone;

$Body .= "\n";

$Body .= "Time: ";

$Body .= $Time;

$Body .= "\n";

 

// send email

$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

 

// redirect to success page

if ($success){

print "<meta http-equiv=\"refresh\" content=\"0;URL=thank_you.html\">";

}

else{

print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

}

?>

Edited by splinter_1234
Link to comment
Share on other sites

The problem stems from the fact that DreamWeaver only added Javascript validation of the form, which means that anything that doesn't parse JS completely avoids the validation. Which allows it to submit a completely empty form.

To avoid this you will need to test the values in the PHP script, and if they are empty () add an error message to the output (and prevent the mail from being sent).

 

There are lots of examples on this forum on how to properly validate a form, as well as countless examples of working contact forms. So I recommend searching around a bit here, and you should find everything you need. If you're still stuck after reading up on this, please post the updated code and we'll be able to help you. ;)

Link to comment
Share on other sites

What Christian F. said. ;)

 

I wrote some quick code below to get you started. Note that you will probably want to validate further as I am only checking to see if the submitted values are empty. But as I said, hopefully it will get you started and give you an idea on how to proceed.

 


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

$errors = array();

if (empty($Name)) {
$errors[] = 'You must enter a name';
}

if (empty($Phone)) {
$errors[] = 'You must enter a phone number';
}

if (empty($Time)) {
$errors[] = 'You must enter a time';
}

// Done validating
if (empty($errors)) {
// No errors; proceed to send e-mail
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=thank_you.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
}

else {
// Errors occured; loop through them and output
echo '<ul>';

foreach ($errors as $error) {
echo '<li>' . $error . '</li>';
}

echo '</ul>';
}

Link to comment
Share on other sites

Here's a quick and easy example of how to do some really simple input validation:

http://forums.phpfreaks.com/topic/272736-php-form-sanitization-help/#entry1403483

 

It will ensure that no empty data is posted. Though, it will still accept any type of content, so you could/will still get mails filled with just random input. At least it should stop the empty mails.

 

Edit: Yay.... No "new post" notification strikes again.

 

I'll leave you with a more advanced example of form validation then, so that you have something more to look at. ;)

Edited by Christian F.
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.