splinter_1234 Posted February 3, 2013 Share Posted February 3, 2013 (edited) 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 February 3, 2013 by splinter_1234 Quote Link to comment https://forums.phpfreaks.com/topic/273980-php-contact-form-receiving-blank-emails/ Share on other sites More sharing options...
Christian F. Posted February 3, 2013 Share Posted February 3, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273980-php-contact-form-receiving-blank-emails/#findComment-1409898 Share on other sites More sharing options...
splinter_1234 Posted February 3, 2013 Author Share Posted February 3, 2013 Thanks for your reply, unfortunately after spending a few hours looking, I am struggling Quote Link to comment https://forums.phpfreaks.com/topic/273980-php-contact-form-receiving-blank-emails/#findComment-1409920 Share on other sites More sharing options...
Andy123 Posted February 3, 2013 Share Posted February 3, 2013 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/273980-php-contact-form-receiving-blank-emails/#findComment-1409939 Share on other sites More sharing options...
Christian F. Posted February 3, 2013 Share Posted February 3, 2013 (edited) 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 February 3, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/273980-php-contact-form-receiving-blank-emails/#findComment-1409943 Share on other sites More sharing options...
splinter_1234 Posted February 9, 2013 Author Share Posted February 9, 2013 Thanks very much guys, i'll give it a go and report back! Quote Link to comment https://forums.phpfreaks.com/topic/273980-php-contact-form-receiving-blank-emails/#findComment-1411263 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.