title: PHP form submission to self failed to load
body: <p>I have customized a php form code by adding a captcha which I expected to show the errors on the form itself instead of using the die command to show it to another page or pop up dialogue. I used PHP sesssion instead of Ajax. I am having following problems.</p>
  
  <p>1) After clicking the submit button, the browser address bar shows the processing php file path but it fails to load the form again following the header command, just showing a blank screen. Previously, it was able to load the form with error codes and the captcha worked just fine. I don't know what went wrong.</p>
  
  <p>2) strangely, if I press the browser back button, the form loads as it was left. And when i reload the form to start again, instead of clearing the form, it starts to show error message but only one error message at a time! (but I want to show all error messages at once if multiple fields are empty or filled with wrong type of character.) Why would a form process data up on refreshing the page? Shouldn't it only happen when the submit button is pressed?</p>
  
  <p>3) Despite the loading problem, I am still getting email, with empty fields!</p>
  
  <p>Please help.</p>
  
  <p>The contactform.php code is given below:</p>
  
  <pre><code>&lt;?php
  session_start();
  ?&gt;
  &lt;form action="php/contact.php" method="post" id="frmContact"&gt;
  
  &lt;h5 style="color: #ffffff;"&gt;What's your Name?&lt;/h5&gt;
  &lt;input name="txtName" type="text" class="txbx" value="" /&gt;
  &lt;?php echo $_SESSION[lmsg_name]; ?&gt;&lt;br /&gt;
  
  &lt;h5 style="color: #ffffff;"&gt;What's your Email?&lt;/h5&gt;
  &lt;input name="txtEmail" type="text" class="txbx" value="" /&gt;
  &lt;?php echo $_SESSION[lmsg_email]; ?&gt;&lt;br /&gt;
  
  &lt;h5 style="color: #ffffff;"&gt;Email Subject?&lt;/h5&gt;
  &lt;input name="txtSubject" type="text" class="txbx" value="" /&gt;
  &lt;?php echo $_SESSION[lmsg_subject]; ?&gt;&lt;br /&gt;
  
  &lt;h5 style="color: #ffffff;"&gt;Message to us&lt;/h5&gt;
  &lt;textarea name="txtText" class="txbx era" placeholder="" &gt;&lt;/textarea&gt;
  &lt;?php echo $_SESSION[lmsg_text]; ?&gt;&lt;br /&gt;
  
  &lt;h5 style="color: #ffffff;"&gt;Enter verification code&lt;/h5&gt;
  &lt;img src="php/captcha.php" style="float:left; margin-left: 11px; margin-right: 10px;   margin-top: 5px;" &gt;
  &lt;input type="text" name="vercode" class="txbxcap" /&gt;
  &lt;?php echo $_SESSION[lmsg_incode]; ?&gt;&lt;br /&gt;
  
  &lt;input name="" type="submit" class="sendbtn" value="SEND MESSAGE" id="btnSend"/&gt;
  
  &lt;/form&gt;
  &lt;?php
  $_SESSION[lmsg_name] ='';
  $_SESSION[lmsg_email] ='';
  $_SESSION[lmsg_subject] ='';
  $_SESSION[lmsg_text] ='';
  $_SESSION[lmsg_incode] ='';
  ?&gt;
  </code></pre>
  
  <p>The captcha.php code is given below:</p>
  
  <pre><code>&lt;?php 
  session_start(); 
  $text = rand(10000,99999); 
  $_SESSION["vercode"] = $text; 
  $height = 38; 
  $width = 55; 
  
  $image_p = imagecreate($width, $height); 
  $black = imagecolorallocate($image_p, 0, 0, 0); 
  $white = imagecolorallocate($image_p, 255, 255, 255); 
  $font_size = 16; 
  
  imagestring($image_p, $font_size, 5, 5, $text, $white); 
  imagejpeg($image_p, null, 80); 
  ?&gt;
  </code></pre>
  
  <p>The processing contact.php is given below:</p>
  
  <pre><code>&lt;?php
  
  session_start(); 
  
  $Name = htmlspecialchars($_POST['txtName']);
  $Email = htmlspecialchars($_POST['txtEmail']);
  $Subject = htmlspecialchars($_POST['txtSubject']);
  $Text = htmlspecialchars($_POST['txtText']);
  
  $string_exp = "/^[A-Za-z .'-]+$/";
  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  
  
  if (empty ($Name) || !preg_match($string_exp,$Name)) { 
  $error_name = '&lt;div style="color: #e44558; float:left; margin-left: 11px;"&gt;*Please provide a name.&lt;br /&gt;&lt;/div&gt;';
  $_SESSION[lmsg_name] = $error_name;
  header('Location:http:../contactform.php');
  }
  
  elseif (empty ($Email) || !preg_match($email_exp,$Email)) { 
  $error_email = '&lt;div style="color: #e44558; float:left; margin-left: 11px;"&gt;*Please enter a valid email address.&lt;br /&gt;&lt;/div&gt;';
  $_SESSION[lmsg_email] = $error_email;
  header('Location:http:../contactform.php');
  }
  
  elseif (empty ($Subject) ||!preg_match($string_exp,$Subject)) {
  $error_subject = '&lt;div style="color: #e44558; float:left; margin-left: 11px;"&gt;*Please provide a subject.&lt;br /&gt;&lt;/div&gt;';
  $_SESSION[lmsg_subject] = $error_subject;
  header('Location:http:../contactform.php');
  }
  
  elseif (empty ($Text) ||!preg_match($string_exp,$Text)) {
  $error_text = '&lt;div style="color: #e44558; float:left; margin-left: 11px;"&gt;*Please write few words...&lt;br /&gt;&lt;/div&gt;';
  $_SESSION[lmsg_text] = $error_text;
  header('Location:http:../contactform.php');
  }
  
  elseif ($_POST["vercode"] != $_SESSION["vercode"] OR $_SESSION["vercode"]=='') { 
  $error_code = '&lt;div style="color: #e44558; float:left; margin-left: 11px;"&gt;*Incorrect verification code. Please try again.&lt;br /&gt;&lt;/div&gt;';
  $_SESSION[lmsg_incode] = $error_code;
  header('Location:http:../contactform.php');
  }
  
  else {
  
  //create email
  $email_to = "x@y.com";
  $email_subject = "Visitor Query: ". $Subject;
  $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 .= "Name: ".clean_string($Name)."\n";
  $email_message .= "Email: ".clean_string($Email)."\n";
  $email_message .= "Message: ".clean_string($Text)."\n";
  
  
  // create email headers
  $headers = 'From: '.$Email."\r\n".
  'Reply-To: '.$Email."\r\n" .
  'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers);     
  
  
  header('location:../contact_thnx.html');
  }
  ?&gt;
  </code></pre>
  
  <p>Thank you in advance.</p>