mathewballard Posted October 26, 2009 Share Posted October 26, 2009 So I built my first working PHP "Contact Us" like form. But, I have been trying to now make the form Validate for empty fields when the user hits the submit button. Problem is when I added in the code and tried to mesh the two together it doesn't work. First I had errors and fixed those, but now I just get a blank white screen and an email doesn't send. I will admit that this is my first time working with PHP and I don't have a full understanding yet. Needless to say, I can't seem to figure it out and just need some help. <?php if ( $_POST['submit'] ) { $error = ''; if ( $_POST['name'] == '' ) { $error = 'Error: Please enter your name'; } if ( $_POST['phonenumber'] == '' ) { $error = 'Error: Please enter your phone number'; } if ( $_POST['appointmentdate'] == '' ) { $error = 'Error: Please enter a date'; } if ( $_POST['appointmenttime'] == '' ) { $error = 'Error: Please enter an appointment time'; } if ( $_POST['appointmenttype'] == '' ) { $error = 'Error: Please enter an appointment type'; } if ( !$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."; } } } ?> Code for Form: <div id="content2" align="left"> <?php if ( $error ) { ?> <p class="error"><?=$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="<?=$_POST['name'];?>" /><br /><br /> <label for="phonenumber"><b>Phone Number</b></label> (Starting with areacode)<br /> <input type="text" name="phonenumber" size="20" value="<?=$_POST['phonenumber'];?>" /><br /><br /> <label for="apptdate"><b>Appointment Date</b></label><br /> <input type="text" name="appointmentdate" size="20" value="<?=$_POST['appointmentdate'];?>" /><br /><br /> <label for="appttime"><b>Appointment Time</b></label><br /> <input type="text" name="appointmenttime" size="20" value="<?=$_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" ><?=$_POST['appointmenttype'];?></textarea><br /><br /> <input type="submit" value="submit" name="submit" /><br /> </form> </div> Quote Link to comment https://forums.phpfreaks.com/topic/178994-contact-form-not-working-when-i-add-validation-codes/ Share on other sites More sharing options...
MadTechie Posted October 26, 2009 Share Posted October 26, 2009 try this <?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."; } } } ?> <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> Quote Link to comment https://forums.phpfreaks.com/topic/178994-contact-form-not-working-when-i-add-validation-codes/#findComment-944360 Share on other sites More sharing options...
mathewballard Posted October 26, 2009 Author Share Posted October 26, 2009 Ok, I will give that a try. But, I do have one question. Do I need to put all of it into the same document or do I keep the two separate like I have them currently. Quote Link to comment https://forums.phpfreaks.com/topic/178994-contact-form-not-working-when-i-add-validation-codes/#findComment-944361 Share on other sites More sharing options...
mathewballard Posted October 26, 2009 Author Share Posted October 26, 2009 Disregard my last post. Ok, so what you did now has the form working as far as sending the email goes, but it doesn't check for errors, or at least show any errors. try this <?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."; } } } ?> <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> Quote Link to comment https://forums.phpfreaks.com/topic/178994-contact-form-not-working-when-i-add-validation-codes/#findComment-944364 Share on other sites More sharing options...
MadTechie Posted October 26, 2009 Share Posted October 26, 2009 If they are not in the same file then your need so pass $errors another way Quote Link to comment https://forums.phpfreaks.com/topic/178994-contact-form-not-working-when-i-add-validation-codes/#findComment-944633 Share on other sites More sharing options...
mathewballard Posted October 26, 2009 Author Share Posted October 26, 2009 If I'm understanding correctly you are saying that it would be best for me to compile all the code into one php file instead of having two PHP files one of which is pretty much just the physical form. If they are not in the same file then your need so pass $errors another way Quote Link to comment https://forums.phpfreaks.com/topic/178994-contact-form-not-working-when-i-add-validation-codes/#findComment-944825 Share on other sites More sharing options...
MadTechie Posted October 26, 2009 Share Posted October 26, 2009 IMHO, i don't see the point in posting to another file, when its not needed, Keeping everything your form needs in one file, makes more logical sense, for something like a contact us, then i would keep it all together (including any classes of course) Quote Link to comment https://forums.phpfreaks.com/topic/178994-contact-form-not-working-when-i-add-validation-codes/#findComment-944848 Share on other sites More sharing options...
mathewballard Posted October 26, 2009 Author Share Posted October 26, 2009 The tutorial that I had looked at to initially build this thing had things in two separate files, so I was under the impression it needed to be in two files. IMHO, i don't see the point in posting to another file, when its not needed, Keeping everything your form needs in one file, makes more logical sense, for something like a contact us, then i would keep it all together (including any classes of course) Quote Link to comment https://forums.phpfreaks.com/topic/178994-contact-form-not-working-when-i-add-validation-codes/#findComment-944872 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.