mathewballard Posted November 19, 2009 Share Posted November 19, 2009 I'm trying to set up this contact form with validation for the first time. I've done the research and no matter what I try it seems to always just go to the thank you page. I'm out of ideas and need some expert help. <div id="content2" align="left"> <?php if ( !empty($error) ) { ?> <p class="error"><?php echo $error;?></p> <?php } ?> <form method="post" action="appointment.php" name="apptform"> <label for="name"><b>Name</b></label><br /> <input type="text" name="name" size="20" value="<?php echo $_POST['name'];?>" /><br /><br /> <label for="phonenumber"><b>Phone Number</b></label> (Starting with areacode)<br /> <input type="text" name="phonenumber" size="20" value="<?php echo $_POST['phonenumber'];?>" /><br /><br /> <label for="apptdate"><b>Appointment Date</b></label><br /> <input type="text" name="appointmentdate" size="20" value="<?php echo $_POST['appointmentdate'];?>" /><br /><br /> <label for="appttime"><b>Appointment Time</b></label><br /> <input type="text" name="appointmenttime" size="20" value="<?php echo $_POST['appointmenttime'];?>" /><br /><br /> <label for="appttype"><b>Apointment Type</b></label> (i.e. Annual Checkup, drilling, etc.)<br /> <textarea rows="10" name="appointmenttype" cols="30" ><?php echo $_POST['appointmenttype'];?></textarea><br /><br /> <input type="submit" value="submit" name="submit" /><br /> </form> </div> <div id="footer"> <img src="images/bottom.png" /><br /> DeVries Family Dentistry ©2009 </div> </div> <?php if ( isset($_POST['submit']) ) { $error = ''; if ( empty($_POST['name']) ) { $error .= "Error: Please enter your name<br />\n"; } if ( empty($_POST['phonenumber']) ) { $error .= "Error: Please enter your phone number<br />\n"; } if ( empty($_POST['appointmentdate']) ) { $error .= "Error: Please enter a date<br />\n"; } if ( empty($_POST['appointmenttime']) ) { $error .= "Error: Please enter an appointment time<br />\n"; } if ( empty($_POST['appointmenttype']) ) { $error .= "Error: Please enter an appointment type<br />\n"; } if ( !empty($error) ) { $to = "bizsumpark182@gmail.com"; $subject = "Someone Has Requested An Appointment!"; $name_field = $_POST['name']; $phonenumber_field = $_POST['phonenumber']; $appointmentdate_field = $_POST['appointmentdate']; $appointmenttime_field = $_POST['appointmenttime']; $appointmenttype_field = $_POST['appointmenttype']; $body = "From: $name_field\n Phone Number: $phonenumber_field\n Requested Appointment Date:\n $appointmentdate_field\n Requested Appointment Time:\n $appointmenttime_field\n Requested Appointment Type:\n $appointmenttype_field"; if (mail($to, $subject, $body)) { header("location: http://www.mathewballard.com/devriesdental/thankyou.html"); } else { echo "Sorry, there was an error processing your request."; } } } ?> Link to PHP file. http://www.mathewballard.com/devriesdental/appointmentform.php Quote Link to comment https://forums.phpfreaks.com/topic/182093-contact-form-doesnt-validate-goes-to-thank-you-page-no-matter-what/ Share on other sites More sharing options...
premiso Posted November 19, 2009 Share Posted November 19, 2009 Not sure why it is doing that, but an issue I do see is that no one will ever see the error as it is before the error checks and is not being stored/called via session or appended on as get data. That alone is a logic flaw, try fixing that and maybe it will solve the main problem of the form redirecting when it should not... Quote Link to comment https://forums.phpfreaks.com/topic/182093-contact-form-doesnt-validate-goes-to-thank-you-page-no-matter-what/#findComment-960628 Share on other sites More sharing options...
thewooleymammoth Posted November 19, 2009 Share Posted November 19, 2009 you have your else in the wrong spot. Right now you are executing the mail function and if the function is unsuccessful then you give the error, however the mail function rarely fails. change the following code, i beleive its your problem <?php if ( !empty($error) ) { $to = "bizsumpark182@gmail.com"; $subject = "Someone Has Requested An Appointment!"; $name_field = $_POST['name']; $phonenumber_field = $_POST['phonenumber']; $appointmentdate_field = $_POST['appointmentdate']; $appointmenttime_field = $_POST['appointmenttime']; $appointmenttype_field = $_POST['appointmenttype']; $body = "From: $name_field\n Phone Number: $phonenumber_field\n Requested Appointment Date:\n $appointmentdate_field\n Requested Appointment Time:\n $appointmenttime_field\n Requested Appointment Type:\n $appointmenttype_field"; if (mail($to, $subject, $body)) { header("location: http://www.mathewballard.com/devriesdental/thankyou.html"); } } else { echo "Sorry, there was an error processing your request."; } Quote Link to comment https://forums.phpfreaks.com/topic/182093-contact-form-doesnt-validate-goes-to-thank-you-page-no-matter-what/#findComment-960631 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 yep just take one of the } after the else and add it right before the else. Quote Link to comment https://forums.phpfreaks.com/topic/182093-contact-form-doesnt-validate-goes-to-thank-you-page-no-matter-what/#findComment-960649 Share on other sites More sharing options...
mathewballard Posted November 19, 2009 Author Share Posted November 19, 2009 I did that and it still doesn't work right. Also, I'm wondering if I should have this code: <?php if ( isset($_POST['submit']) ) { $error = ''; if ( empty($_POST['name']) ) { $error .= "Error: Please enter your name<br />\n"; } if ( empty($_POST['phonenumber']) ) { $error .= "Error: Please enter your phone number<br />\n"; } if ( empty($_POST['appointmentdate']) ) { $error .= "Error: Please enter a date<br />\n"; } if ( empty($_POST['appointmenttime']) ) { $error .= "Error: Please enter an appointment time<br />\n"; } if ( empty($_POST['appointmenttype']) ) { $error .= "Error: Please enter an appointment type<br />\n"; } if ( isset($_POST['submit']) ) { $to = "bizsumpark182@gmail.com"; $subject = "Someone Has Requested An Appointment!"; $name_field = $_POST['name']; $phonenumber_field = $_POST['phonenumber']; $appointmentdate_field = $_POST['appointmentdate']; $appointmenttime_field = $_POST['appointmenttime']; $appointmenttype_field = $_POST['appointmenttype']; $body = "From: $name_field\n Phone Number: $phonenumber_field\n Requested Appointment Date:\n $appointmentdate_field\n Requested Appointment Time:\n $appointmenttime_field\n Requested Appointment Type:\n $appointmenttype_field"; if (mail($to, $subject, $body)) { header("location: http://www.mathewballard.com/devriesdental/thankyou.html"); } } else { echo "Sorry, there was an error processing your request."; } } ?> In the same file as the form or should I have it in a different file? When I have form action (<form method="post" action="#") set for within the file I get a header error? I apologize for being such a noob. But, I'm a designer who has been learning different codes for the last couple of years and in all honesty I prefer to create instead of code, but I have to know how to code (other then html/css) to be successful. yep just take one of the } after the else and add it right before the else. Quote Link to comment https://forums.phpfreaks.com/topic/182093-contact-form-doesnt-validate-goes-to-thank-you-page-no-matter-what/#findComment-960681 Share on other sites More sharing options...
mathewballard Posted November 19, 2009 Author Share Posted November 19, 2009 Never mind about the header error. I realized that I need to have the code that processes the form before the html begins. But, do still need help in figuring out why my form won't validate correctly. Quote Link to comment https://forums.phpfreaks.com/topic/182093-contact-form-doesnt-validate-goes-to-thank-you-page-no-matter-what/#findComment-960687 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 just do action="pagename.php" or whatever. its fine to have it on the same page as long as ur using the isset() function is it sending the mail? or just redirecting? cause this should work: <?php if ( isset($_POST['submit']) ) { $error = ''; if ( empty($_POST['name']) ) { $error .= "Error: Please enter your name<br />\n"; } if ( empty($_POST['phonenumber']) ) { $error .= "Error: Please enter your phone number<br />\n"; } if ( empty($_POST['appointmentdate']) ) { $error .= "Error: Please enter a date<br />\n"; } if ( empty($_POST['appointmenttime']) ) { $error .= "Error: Please enter an appointment time<br />\n"; } if ( empty($_POST['appointmenttype']) ) { $error .= "Error: Please enter an appointment type<br />\n"; } if ( !empty($error) ) { $to = "bizsumpark182@gmail.com"; $subject = "Someone Has Requested An Appointment!"; $name_field = $_POST['name']; $phonenumber_field = $_POST['phonenumber']; $appointmentdate_field = $_POST['appointmentdate']; $appointmenttime_field = $_POST['appointmenttime']; $appointmenttype_field = $_POST['appointmenttype']; $body = "From: $name_field\n Phone Number: $phonenumber_field\n Requested Appointment Date:\n $appointmentdate_field\n Requested Appointment Time:\n $appointmenttime_field\n Requested Appointment Type:\n $appointmenttype_field"; if (mail($to, $subject, $body)) { header("location: http://www.mathewballard.com/devriesdental/thankyou.html"); } } else { echo "Sorry, there was an error processing your request.";} } ?> Quote Link to comment https://forums.phpfreaks.com/topic/182093-contact-form-doesnt-validate-goes-to-thank-you-page-no-matter-what/#findComment-960688 Share on other sites More sharing options...
mathewballard Posted November 19, 2009 Author Share Posted November 19, 2009 Yes, it is going to be sending the info to an email as well as redirecting. Actually, I just got it to work a minute ago. You can see it here: http://www.mathewballard.com/devriesdental/appointmentform.php. But, now I need to figure out how to get the errors to appear beside each item. And maybe do something like change the text to red. And to get the "sorry there was an error processing your request" message to appear just above the form. Any advice would be great. I'm a quick learner, and pick up on things pretty fast once I learn how to initially do it. just do action="pagename.php" or whatever. its fine to have it on the same page as long as ur using the isset() function is it sending the mail? or just redirecting? cause this should work: <?php if ( isset($_POST['submit']) ) { $error = ''; if ( empty($_POST['name']) ) { $error .= "Error: Please enter your name<br />\n"; } if ( empty($_POST['phonenumber']) ) { $error .= "Error: Please enter your phone number<br />\n"; } if ( empty($_POST['appointmentdate']) ) { $error .= "Error: Please enter a date<br />\n"; } if ( empty($_POST['appointmenttime']) ) { $error .= "Error: Please enter an appointment time<br />\n"; } if ( empty($_POST['appointmenttype']) ) { $error .= "Error: Please enter an appointment type<br />\n"; } if ( !empty($error) ) { $to = "bizsumpark182@gmail.com"; $subject = "Someone Has Requested An Appointment!"; $name_field = $_POST['name']; $phonenumber_field = $_POST['phonenumber']; $appointmentdate_field = $_POST['appointmentdate']; $appointmenttime_field = $_POST['appointmenttime']; $appointmenttype_field = $_POST['appointmenttype']; $body = "From: $name_field\n Phone Number: $phonenumber_field\n Requested Appointment Date:\n $appointmentdate_field\n Requested Appointment Time:\n $appointmenttime_field\n Requested Appointment Type:\n $appointmenttype_field"; if (mail($to, $subject, $body)) { header("location: http://www.mathewballard.com/devriesdental/thankyou.html"); } } else { echo "Sorry, there was an error processing your request.";} } ?> Quote Link to comment https://forums.phpfreaks.com/topic/182093-contact-form-doesnt-validate-goes-to-thank-you-page-no-matter-what/#findComment-960693 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 you need to make and extra column next to each input text thing and then echo $errormessage. set the error messages before the form is. if there is no error message then there will be nothing next to the text feilds. Quote Link to comment https://forums.phpfreaks.com/topic/182093-contact-form-doesnt-validate-goes-to-thank-you-page-no-matter-what/#findComment-960712 Share on other sites More sharing options...
mathewballard Posted November 19, 2009 Author Share Posted November 19, 2009 Could I bother you for an example? Not with what I'm doing, just something to where I can what you are saying. you need to make and extra column next to each input text thing and then echo $errormessage. set the error messages before the form is. if there is no error message then there will be nothing next to the text feilds. Quote Link to comment https://forums.phpfreaks.com/topic/182093-contact-form-doesnt-validate-goes-to-thank-you-page-no-matter-what/#findComment-960719 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 http://www.socialemo.com/xxxxx/register.php if u type something into the first two feilds that is like bad it shows(its javascript there) but that shoes the columns . also. if u echo a variable that has nothing in it nothing will be echoes but when u use php to do the error messages like i assume u were refering to. u just echo the error messages next to the input text feilds. like td td Quote Link to comment https://forums.phpfreaks.com/topic/182093-contact-form-doesnt-validate-goes-to-thank-you-page-no-matter-what/#findComment-960755 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.