khujo56 Posted June 29, 2007 Share Posted June 29, 2007 Hi guys i created a contact form in html and a code in php to send the info to my e-mail account. the problem i have is that i want an error to show if nothing or some of the info is not filled in when i press the submit button. if i click the submit button with nothing filled out, it says the info has been sent to my e-mail account. can anyone look at my code and see what i'm doing wrong. below is the php code. <?php if(isset($_POST['submit'])) { $to = "[email protected]"; $subject = "Contact Info"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; $errors = ''; if ( empty($name_field) ) $errors .= 'fill out name<br/>'; if ( empty($email_field) ) $errors .= 'fill out email<br/>'; if ( empty($message) ) $errors .= 'fill out message<br/>'; $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; if ( !empty($errors) ) { echo $errors; } else { mail($to, $subject, $body); echo "Data has been submitted to $to!"; } ?> Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/ Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 if(mail($to, $subject, $body) { echo "Data has been submitted to $to!"; } else { echo 'failed'; } and to verify if forms are filled up completely use the isset() Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-285464 Share on other sites More sharing options...
khujo56 Posted June 29, 2007 Author Share Posted June 29, 2007 sorry i should have said someone else wrote this script. do i just add what you put Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-285465 Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 yes Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-285466 Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 <?php if(isset($_POST['submit'])) { $to = "[email protected]"; $subject = "Contact Info"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; $errors = ''; if (!isset($name_field)) { $errors .= 'fill out name'; } if (!isset($email_field)) { $errors .= 'fill out email'; } if (!isset($message)) { $errors .= 'fill out message'; } $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; if ( !empty($errors) ) { echo $errors; } else { if(mail($to, $subject, $body) { echo "Data has been submitted to $to!"; } } } ?> practice this way of coding Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-285467 Share on other sites More sharing options...
soycharliente Posted June 29, 2007 Share Posted June 29, 2007 IMHO: Using empty() is fine. I do it all the time. Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-285470 Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 <?php $var = 0; // Evaluates to true because $var is empty if (empty($var)) { echo '$var is either 0, empty, or not set at all'; } // Evaluates as true because $var is set if (isset($var)) { echo '$var is set even though it is empty'; } ?> see the diff. any way it base on the needs Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-285476 Share on other sites More sharing options...
soycharliente Posted June 29, 2007 Share Posted June 29, 2007 If someone typed 0, NULL, or "" for any input to a form I made, I'd want it to return that it was blank. Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-285516 Share on other sites More sharing options...
khujo56 Posted June 29, 2007 Author Share Posted June 29, 2007 Hi i've tried the new script and when i put nothing in the form and click submit, all i get is a blank page. there is no message saying to fill in the form. any idea what's causing this Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286252 Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 can we have the new code Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286253 Share on other sites More sharing options...
khujo56 Posted June 29, 2007 Author Share Posted June 29, 2007 Hi Teng Actually i got the code from your prior post. here it is below. here is the page where the form is on. http://www.iheartvacation.com/index-4.html here is the code <?php if(isset($_POST['submit'])) { $to = "[email protected]"; $subject = "Contact Info"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; $errors = ''; if (!isset($name_field)) { $errors .= 'fill out name'; } if (!isset($email_field)) { $errors .= 'fill out email'; } if (!isset($message)) { $errors .= 'fill out message'; } $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; if ( !empty($errors) ) { echo $errors; } else { if(mail($to, $subject, $body) { echo "Data has been submitted to $to!"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286255 Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 $to = "[email protected]"; $subject = "Contact Info"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; $errors = ''; if (!isset($name_field)) { $errors .= 'fill out name'; } if (!isset($email_field)) { $errors .= 'fill out email'; } if (!isset($message)) { $errors .= 'fill out message'; } $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; if ( !empty($errors) ) { echo $errors; } else { if(mail($to, $subject, $body)) { echo "Data has been submitted to $to!"; } } try that first i remove the condition isset post coz that maybe coz the blank any way try that and tell me what happen coz it works fine here Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286258 Share on other sites More sharing options...
khujo56 Posted June 29, 2007 Author Share Posted June 29, 2007 yeah it' still does not work..it's odd Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286291 Share on other sites More sharing options...
teng84 Posted June 29, 2007 Share Posted June 29, 2007 i see the site and its still blank mmm!! try to echo something anywhere on that page and see if there will be an output because that works fine on me say echo 'test 101'; then check if that will appear i view the source of the site ant thats blank alco so try echoing something that will help you debug Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286294 Share on other sites More sharing options...
khujo56 Posted June 30, 2007 Author Share Posted June 30, 2007 sorry i'm a php newbie..i'm not sure what u mean by that Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286363 Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 put this anywhere on your mail.php echo 'test 101'; thats the 1st step to debug Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286367 Share on other sites More sharing options...
khujo56 Posted June 30, 2007 Author Share Posted June 30, 2007 ok i deleted the script and put echo 'test 101'; and clicked submit, it went to another page with test 101 on it Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286383 Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 $to = "[email protected]"; $subject = "Contact Info"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; $errors = ''; if (!isset($name_field)) { $errors .= 'fill out name'; } if (!isset($email_field)) { $errors .= 'fill out email'; } if (!isset($message)) { $errors .= 'fill out message'; } $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; if ( !empty($errors) ) { echo $errors; } else { if(mail($email_field , $subject, $message)) { echo "Data has been submitted to $to!"; } } that will work now ^^ if(mail($to, $subject, $body))// you dont change this $to, $subject, $body this variable are not set Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286392 Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 i know y you dont get the message $to = "[email protected]"; $subject = "Contact Info"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; $errors = ''; if (!isset($name_field)) { $errors .= 'fill out name'; } if (!isset($email_field)) { $errors .= 'fill out email'; } if (!isset($message)) { $errors .= 'fill out message'; } $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; if ($errors!='') { echo $errors; } else { if(mail($email_field , $subject, $message)) { echo "Data has been submitted to $to!"; } } just paste that Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286397 Share on other sites More sharing options...
khujo56 Posted June 30, 2007 Author Share Posted June 30, 2007 hey, yeah i've tried the new code, still gettting the white page when i click submit. so odd why it does that. could it be the form itself that's messing up..everything looks ok on the form. Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-286576 Share on other sites More sharing options...
khujo56 Posted July 3, 2007 Author Share Posted July 3, 2007 can anyone please help me with this..it's so close to working but something is not functioning properly Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-288752 Share on other sites More sharing options...
scarhand Posted July 3, 2007 Share Posted July 3, 2007 Try this: if(isset($_POST['submit'])) { if ( empty($_POST['name']) ) { exit("You must fill in your name!"); } if ( empty($_POST['email']) ) { exit("You must fill in your email address!"); } if ( empty($_POST['message']) ) { exit("You must write a message to send!"); } } Or, if you want the form to appear under the error message than echo it, like so: if(isset($_POST['submit'])) { if ( empty($_POST['name']) ) { echo 'You must fill in your name!'; } if ( empty($_POST['email']) ) { echo 'You must fill in your email address!'; } if ( empty($_POST['message']) ) { echo 'You must write a message to send!'; } } You can use echo or exit, whichever you prefer. I am a PHP newbie too, but if you're unsure how to use echo or exit look it up on PHP.net Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-288775 Share on other sites More sharing options...
khujo56 Posted July 3, 2007 Author Share Posted July 3, 2007 thanks that helped. is there a way you can write a message that is has been posted, then have it re-directed to another page..like back to the main page ? Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-288792 Share on other sites More sharing options...
scarhand Posted July 3, 2007 Share Posted July 3, 2007 thanks that helped. is there a way you can write a message that is has been posted, then have it re-directed to another page..like back to the main page ? You would have to simply echo the variable you stated for the posted message in order to display it. example: echo $message; Redirection to another page can be done using header. example: header('Location: http://www.placeiwanttoredirectto.com/'); this must be placed before the <html> tag in order to work properly though. Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-288799 Share on other sites More sharing options...
xyn Posted July 3, 2007 Share Posted July 3, 2007 Try this. <?php if(isset($_POST['name']) && empty($_POST['name'])) { $error[] = "Post your name"; } if(isset($_POST['email']) && empty($_POST['email'])) { $error[] = "Post your name"; } if(isset($_POST['message']) && empty($_POST['message'])) { $error[] = "Post your name"; } if(is_array($error)) { echo("errors:<br>"); foreach($error as $x => $i) { echo("- ".$i."<br>"); } } else { // submit mail stuff here! } ?> Link to comment https://forums.phpfreaks.com/topic/57653-contact-form/#findComment-288876 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.