Jump to content

Recommended Posts

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>

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>

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>

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

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)

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)

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.