PHP表单难以理解$ _POST

Hey Guys this should be probably really simple I am just missing a step.

So I have a form and I want to make sure all values are added before moving to the next page that processes my values and sends them to my email. I also have error messages if someone does not add a value. The error messages display on the reloading of the page.

The problem I have is that you have to reload the page for the Superglobal Post to contain any values. For some reason it does not add them until the page is reloaded. So what happens is if you fill all 5 input fields you have to reload the page, and then submit again for it to send it to my send_form_email.php script because at that point the values are added. I want it to logically reload if any of the values are empty (which will display error messages telling the user that the input field must have content), and automatically send the user to send_form_email.php if all values have been correctly added.

It's almost pull my hair out so if someone could help me understand what piece of the puzzle I am missing I would be so grateful!

<form name="contactform" method="post" action="<?php echo $value; ?>">

<table width="450px">

<tr>

 <td valign="top">



 </td>

 <td valign="top">

  <input  class="inputs" placeholder="firstname" type="text" name="first_name" maxlength="50" size="30" placeholder="name"><br>
 <span class="error">* <?php echo $firstErr;?></span>
 </td>

</tr>

<tr>

 <td valign="top">



 </td>

 <td valign="top">

  <input  class="inputs" placeholder="lastname" type="text" name="last_name" maxlength="50" size="30">
 <span class="error">* <?php echo $lastErr;?></span>
 </td>

</tr>

<tr>

 <td valign="top">



 </td>

 <td valign="top">

  <input class="inputs" placeholder="email" type="text" name="email" maxlength="80" size="30">
 <span class="error">* <?php echo $emailErr;?></span>
 </td>

</tr>

<tr>

 <td valign="top">



 </td>

 <td valign="top">

  <input  class="inputs" placeholder="telephone" type="text" name="telephone" maxlength="30" size="30">
 <span class="error">* <?php echo $telephoneErr;?></span>
 </td>

</tr>

<tr>

 <td valign="top">


 </td>

 <td valign="top">

  <textarea  class="inputs" placeholder="comments" name="comments" maxlength="1000" cols="25" rows="6"></textarea>
 <span class="error">* <?php echo $commentsErr;?></span>
 </td>

</tr>

<tr>

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

   <input type="submit" value="Submit" style="background-color: #0F6D87;
    font-family: Exo-Light;
    color: #000000;
    width: 75px;
    font-weight: bold;
    border-color: #003D69;
    border-style: outset;
    font-size: .8em;
    box-shadow: 2px 2px 2px rgba(0, 34, 97, 0.6);">
  <INPUT TYPE="RESET">

 </td>

</tr>

</table>

</form>





<?php

if ($_SERVER["REQUEST_METHOD"] == "POST"){

    if (!empty($_POST["first_name"]) && !empty($_POST["last_name"]) && !empty($_POST["email"])  &&  !empty($_POST["telephone"]) && !empty($_POST["comments"])) {

  $value ="http://cdubach.com/inc/send_form_email.php";

    } elseif  (empty($_POST["first_name"]) || empty($_POST["last_name"]) || empty($_POST["email"]) || empty($_POST["telephone"]) || empty($_POST["comments"])){


        $value = "#";
    }



}


    $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



echo $_POST['submit'] . " = Submit <br>";
echo $_POST["first_name"] . " =  First Name  <br>"; 

echo $_POST["last_name"] . " = Last Name  <br>"; 

echo $_POST["email"] . "= Email  <br>"; 

echo $_POST["telephone"] . "= Telephone  <br>"; 


echo $_POST["comments"] . "= Comments <br>"; 

echo var_dump($_Post) . "= Dump <br>";                                               

 echo $value . " = Value <br>" ;                                          



echo  $_SERVER["PHP_SELF"];
header('Content-Type: text/plain');

var_dump(htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES, 'UTF-8'));
echo "<br>";
echo htmlspecialchars("<a href='test'>Test</a>", ENT_XHTML, 'UTF-8');
echo "<br>";
$str = "A 'quote' is <b>bold</b>";

/* */
//convert from utf8
 $str = utf8_decode($str);

 //translate HMTL entities
 $trans = get_html_translation_table(HTML_ENTITIES);
$str = strtr($str, $trans);

echo htmlspecialchars($str);
echo "<br>";
echo htmlentities($str, ENT_QUOTES);

$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;


if ($_SERVER["REQUEST_METHOD"] == "POST") {

     //Check First Name Field if Nothing Post Error
  if (empty($_POST["first_name"])) {
    $firstErr = "Name is required";
  } else {
    $firstErr = test_input($_POST["name"]);
  }


    //Check Last Name Field if Nothing Post Error
    if (empty($_POST["last_name"])) {
    $lastErr = "Last Name is required";
  } else {
    $lastErr = test_input($_POST["last_name"]);
  }


 //Check Email Field if Nothing Post Error
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $emailErr = test_input($_POST["email"]);
  }


 //Check Telephone Field if Nothing Post Error
  if (empty($_POST["telephone"])) {
    $telephoneErr = "Telephone is Required";
  } else {
    $telephoneErr = test_input($_POST["telephone"]);
  }

     //Check Comments Field if Nothing Post Error 
  if (empty($_POST["comments"])) {
    $commentsErr = "Comments is Required";
  } else {
    $commentsErr = test_input($_POST["comments"]);
  }


    //Check Comments
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

?>

The reason you need to reload a second time in order for the script to work is because the action property of the form is not populated on the first run (since $value is not set), on the 2nd run however it is (if the $_POST pass the checks you have set) and is set to http://cdubach.com/inc/send_form_email.php.

You will see this if you check the actual html code in the first and second run.

However this is only one of the problems with your script. Some hints:

  • remove the header('Content-Type: text/plain');, that line instructs the browser to treat the page as text and it will not render it as html.
  • move the whole html after your PHP script - that way the errors messages you have prepared will work as they should
  • finally to resolve your problem with "$_POST only if all ok" condition check the validity of the script with client side JavaScript. On another hand if you have access over the called script (the one that $value points to) you could make the check there and redirect to the form if you should.