Jump to content

Contact Form Doesn't Validate Goes to Thank You Page No Matter What


Recommended Posts

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

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...

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.";
   }

 

 

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.

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.

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.";}
  
}
?>

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.";}
  
}
?>

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.

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

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.