MongooseLover MongooseLover - 5 months ago 9
PHP Question

PHP error outputting erasing part of my html layout

I borrowed this code from how to do a PHP emailer. However, rather than theirs, which was here: http://www.freecontactform.com/email_form.php I decided to use

$PHP_SELF
and do it all in one file.

However, with the error outputting, it was removing my Contact Us thing, table, and footer stuff.

So I thought, must be a dang glitch, I'll just echo out the stuff in the Contact Us, table, and footer and put the error message in between the end of the table and the footer.

However, much to my annoyance, it is STILL erasing it and acting like my new stuff isn't even there. Why is that and what can I do to fix it? Am I better off using two separate files like they did? (I didn't like their approach because there never was a back button once they either entered it right or even if they entered it wrong. )

Even stranger, if I do get it right and doesn't do the error messages, it does output all that stuff above, though way at the bottom and not in the place of the form and form that I had originally.

Finally, when I check my email (I know I have something else above, but in my main one, I use my email address, and also put it it in the from box too. However, it's not showing up in my mailbox.

<html>
<head>
<title> Contact Us </title>
<link rel="stylesheet" type="text/css" href="./main.css">
<link rel="icon" href="./ski_icon.png">
<script src="jquery-3.0.0.min.js" type="text/javascript"></script>
<script>
$( document ).ready(function() {
$("#container").css("height", 1135);
});
</script>
</head>
<body>
<style>
form {
border: 1px solid transparent;
text-align: center;
}
</style>
<div id="content">
<div id="logo">
<img src="./logo.png"></img>
</div>
<div id="container">
<nav id="hor">
<div><a href="index.html"> Home </a> </div>
<div><a href="mountains.html"> The Mountains </a></div>
<div><a href="resorts.html"> The Resorts </a></div>
<div><a href="snowreports.html"> Snow Reports </a></div>
<div><a href="eventsandactivities.html"> Events &amp Activities </a></div>
<div><a href="plantrip.html"> Plan A Trip </a></div>
</nav>
<div id="middle">
<nav id="vert">
<a href="index.html"> Home </a> <br/>
<a href="mountains.html"> The Mountains </a><br/>
<a href="resorts.html"> The Resorts </a><br/>
<a href="snowreports.html"> Snow Reports </a><br/>
<a href="eventsandactivities.html"> Events &amp Activities </a><br/>
<a href="plantrip.html"> Plan A Trip </a>
</nav>
<style>
td {
vertical-align: top;
}
</style>
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "you@yourdomain.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "<div id='text-container'>" ;
echo "<h1>Contact Us</h1>";
echo "<form name='contactform' method='post' action='$_PHP_SELF'>";
echo"<table width='450px'>";
echo "<tr>";
echo" <td valign='top'>";
echo "<label for='first_name'>First Name *</label>";
echo "</td>";
echo " <td valign='top'>";
echo" <input type='text' name='first_name' maxlength='50' size='30'>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo"<td valign='top'>";
echo "<label for='last_name'>Last Name *</label>";
echo "</td>";
echo "<td valign='top'>";
echo "<input type='text' name='last_name' maxlength='50' size='30'>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td valign='top'>";
echo "<label for='email'>Email Address *</label>";
echo "</td>";
echo "<td valign='top'>";
echo "<input type='text' name='email' maxlength='80' size='30'>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td valign='top'>";
echo "<label for='telephone'>Telephone Number</label>";
echo "</td>";
echo "<td valign='top'>";
echo "<input type='text' name='telephone' maxlength='30' size='30'>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td valign='top'>";
echo "<label for='comments'>Comments *</label>";
echo "</td>";
echo "<td valign='top'>";
echo "<textarea name='comments' maxlength='1000' cols='25' rows='6'></textarea>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2' style='text-align:center'>";
echo "<input type='submit' value='Submit'> <a href='http://www.freecontactform.com/email_form.php'>Email Form</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "Bla bla bla";
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div id='footercontainer'>";
echo "<footer>";
echo "Copyright &copy; 2016 Dimaond Peaks Resort, &nbsp; All Rights Reserved. <br/>";
echo "<a href='http://www.allwebpromotion.com/' target='_blank'>Website Design</a> by: All Web Promotion";
echo "</footer>";
echo "</div>";
echo "</div>";
}
// validation expected data exists
if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments']))
{
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php } ?>
<div id="text-container">
<h1>Contact Us</h1>
<form name="contactform" method="post" action="<?php $_PHP_SELF ?>">
<table width="450px">
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name">Last Name *</label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Telephone Number</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit"> <a href="http://www.freecontactform.com/email_form.php">Email Form</a>
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
<div id="footercontainer">
<footer>Copyright &copy; 2016 Dimaond Peaks Resort, &nbsp; All Rights Reserved. <br/><a href="http://www.allwebpromotion.com/" target="_blank">Website Design</a> by: All Web Promotion</footer>
</div>
</body>
</html>

Answer

there is your nice and fixed code =)

<html>
<head>
<title> Contact Us </title>
<link rel="stylesheet" type="text/css" href="./main.css">
<link rel="icon" href="./ski_icon.png">
<script src="jquery-3.0.0.min.js" type="text/javascript"></script>
<script>
 $( document ).ready(function() {

    $("#container").css("height", 1135);


  });

  </script>

</head>
<body>


<style>

form
{
border: 1px solid transparent;
text-align: center;

}
</style>

<div id="content">
<div id="logo">
<img src="./logo.png"></img>
</div>
<div id="container">
<nav id="hor">
<div><a href="index.html"> Home </a> </div>
<div><a href="mountains.html"> The Mountains </a></div>
<div><a href="resorts.html"> The Resorts </a></div>
<div><a href="snowreports.html"> Snow Reports </a></div>
<div><a href="eventsandactivities.html"> Events &amp Activities </a></div>
<div><a href="plantrip.html"> Plan A Trip </a></div>
</nav>
<div id="middle">
<nav id="vert">
<a href="index.html"> Home </a> <br/>
<a href="mountains.html"> The Mountains </a><br/>
<a href="resorts.html"> The Resorts </a><br/>
<a href="snowreports.html"> Snow Reports </a><br/>
<a href="eventsandactivities.html"> Events &amp Activities </a><br/>
<a href="plantrip.html"> Plan A Trip </a>
</nav>

<style>

td
{

vertical-align: top;
}

</style>

<?php

if(isset($_POST['email'])) {



    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "you@yourdomain.com";

    $email_subject = "Your email subject line";





    function died($error) {

        // your error code can go here



       echo "<div id='text-container'>" ;

echo "<h1>Contact Us</h1>";

echo "<form name='contactform' method='post' action='$_PHP_SELF'>";

echo"<table width='450px'>";

echo "<tr>";

echo" <td valign='top'>";

  echo "<label for='first_name'>First Name *</label>";

 echo "</td>";

echo " <td valign='top'>";

 echo" <input  type='text' name='first_name' maxlength='50' size='30' value='".(isset($_POST['first_name'])?$_POST['first_name']:'')."' >";

 echo "</td>";

echo "</tr>";

echo "<tr>";

 echo"<td valign='top'>";

  echo "<label for='last_name'>Last Name *</label>";

 echo "</td>";

 echo "<td valign='top'>";

  echo "<input  type='text' name='last_name' maxlength='50' size='30'  value='".(isset($_POST['last_name'])?$_POST['last_name']:'')."' >";

 echo "</td>";

echo "</tr>";

echo "<tr>";

echo "<td valign='top'>";

echo "<label for='email'>Email Address *</label>";

echo "</td>";

 echo "<td valign='top'>";

 echo "<input  type='text' name='email' maxlength='80' size='30'   value='".(isset($_POST['email'])?$_POST['email']:'')."'  >";

 echo "</td>";

echo "</tr>";

echo "<tr>";

 echo "<td valign='top'>";

 echo "<label for='telephone'>Telephone Number</label>";

echo "</td>";

echo "<td valign='top'>";

echo "<input  type='text' name='telephone' maxlength='30' size='30'     value='".(isset($_POST['telephone'])?$_POST['telephone']:'')."'   >";

echo "</td>";

echo "</tr>";

echo "<tr>";

echo "<td valign='top'>";

echo  "<label for='comments'>Comments *</label>";

echo "</td>";

echo "<td valign='top'>";

 echo "<textarea  name='comments' maxlength='1000' cols='25' rows='6'> ".(isset($_POST['comments'])?$_POST['comments']:'')."  </textarea>";

echo "</td>";

echo "</tr>";

echo "<tr>";

 echo "<td colspan='2' style='text-align:center'>";

  echo "<input type='submit' value='Submit'>   <a href='http://www.freecontactform.com/email_form.php'>Email Form</a>";

 echo "</td>";

echo "</tr>";

echo "</table>";

echo "</form>";

echo "Bla bla bla";

        echo "We are very sorry, but there were error(s) found with the form you submitted. ";

        echo "These errors appear below.<br /><br />";

        echo $error."<br /><br />";

        echo "Please go back and fix these errors.<br /><br />";


      echo "</div>";
echo "</div>";
echo "</div>";

echo "<div id='footercontainer'>";
echo "<footer>";
 echo "Copyright  &copy;  2016   Dimaond Peaks Resort, &nbsp; All Rights Reserved.  <br/>";
echo "<a href='http://www.allwebpromotion.com/' target='_blank'>Website Design</a> by: All Web Promotion";

echo "</footer>";
echo "</div>";
echo "</div>";

    }



    // validation expected data exists

    if(!isset($_POST['first_name']) ||

        !isset($_POST['last_name']) ||

        !isset($_POST['email']) ||

        !isset($_POST['telephone']) ||

        !isset($_POST['comments'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');

    }



    $first_name = $_POST['first_name']; // required

    $last_name = $_POST['last_name']; // required

    $email_from = $_POST['email']; // required

    $telephone = $_POST['telephone']; // not required

    $comments = $_POST['comments']; // required



    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {

    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }

  if(!preg_match($string_exp,$last_name)) {

    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';

  }

  if(strlen($comments) < 2) {

    $error_message .= 'The Comments you entered do not appear to be valid.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }

    $email_message = "Form details below.\n\n";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }



    $email_message .= "First Name: ".clean_string($first_name)."\n";

    $email_message .= "Last Name: ".clean_string($last_name)."\n";

    $email_message .= "Email: ".clean_string($email_from)."\n";

    $email_message .= "Telephone: ".clean_string($telephone)."\n";

    $email_message .= "Comments: ".clean_string($comments)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);
exit;
?>



<!-- include your own success html here -->



Thank you for contacting us. We will be in touch with you very soon.



<?php

}

?>




<div id="text-container">

<h1>Contact Us</h1>

<form name="contactform" method="post" action="<?php $_PHP_SELF ?>">

<table width="450px">

<tr>

 <td valign="top">

  <label for="first_name">First Name *</label>

 </td>

 <td valign="top">

  <input  type="text" name="first_name" maxlength="50" size="30">

 </td>

</tr>

<tr>

 <td valign="top"">

  <label for="last_name">Last Name *</label>

 </td>

 <td valign="top">

  <input  type="text" name="last_name" maxlength="50" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="email">Email Address *</label>

 </td>

 <td valign="top">

  <input  type="text" name="email" maxlength="80" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="telephone">Telephone Number</label>

 </td>

 <td valign="top">

  <input  type="text" name="telephone" maxlength="30" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="comments">Comments *</label>

 </td>

 <td valign="top">

  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>

 </td>

</tr>

<tr>

 <td colspan="2" style="text-align:center">

  <input type="submit" value="Submit">   <a href="http://www.freecontactform.com/email_form.php">Email Form</a>

 </td>

</tr>

</table>

</form>



  </div>
</div>
</div>

<div id="footercontainer">
<footer>
 Copyright  &copy;  2016   Dimaond Peaks Resort, &nbsp; All Rights Reserved.  <br/>
<a href="http://www.allwebpromotion.com/" target="_blank">Website Design</a> by: All Web Promotion

</footer>
</div>
</div>

</body>
</html>