使用PHP_SELF和php验证的html表单 - 提交后,结果显示在新页面上而不显示表单

问题描述:

I am trying to create an html search form using a similar code as posted below.

When I submit the form, I want to submit to PHP_SELF

I want to use php validation code to filter the data.

When I submit the form, I cannot figure out how to get the results to post to a new page without displaying the form.

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "xyz_database";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

}


$showHtml = true;   

$month = $day = $year = "";

$monthErr = $dayErr = $yearErr =  "";

$errorMessage = "Oops..Please correct the item(s) highlighted in red on the form below and re-submit";

 function test_input($data) {  

   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);

   return $data;
 }


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


// Month error & filter check code....

 if (empty($_POST["month"])) {

 $month = "";

 } else {

 $month = test_input($_POST["month"]);

 if (!preg_match("/^[a-zA-Z ]*$/",$month)) {

  $monthErr = "An invalid entry has been detected. Please reset this form and re-submit.";

  }
  }


 // Day error & filter check code....

   if (empty($_POST["day"])) {

   $day = "";

   } else {

   $day = test_input($_POST["day"]);

   if (!is_numeric($day)) {

  $dayErr = "Day Found - An invalid entry has been detected. Please reset this form and re-submit.";

 }
 }

  // Year error & filter check code....

  if (empty($_POST["year"])) {

  $year = "";

  } else {

  $year = test_input($_POST["year"]);

  if (!is_numeric($year)) {

  $yearErr = "Year Found - An invalid entry has been detected. Please reset this form and re-submit.";


  }
   }


 if (empty($monthErr) and empty($dayErr)  and empty($yearErr)) {


 $showHtml = false;  


$value1 = $_POST['month'];
$value2 = $_POST['day'];
$value3 = $_POST['year'];


 $sql = "SELECT * FROM xyz_test_database WHERE month = ('$value1') AND day =     ('$value2') AND year = ('$value3')";

 $result = $conn->query($sql);


if ($result->num_rows > 0) {echo "<br><br><h2>Search Results</h2>
                       <table><tr>
                                  <th>ID</th> 
                                  <th>Time Stamp</th>
                                  <th>Month</th>                                 
                                  <th>Day</th>
                                  <th>Year</th>
                             </tr>";

 // output data of each row

while($row = $result->fetch_assoc()) {
echo "<tr>
          <td>".$row["id"]."</td>
          <td>".$row["time_stamp"]."</td>
          <td>".$row["month"]."</td>
          <td>".$row["day"]."</td>
          <td>".$row["year"]."</td>
      </tr>";
 }

 echo "</table>";

 } else {


  echo "<p id='no_results'>Sorry - No Results Found :( </p>";

  }
  }
  }


  $conn->close();

  exit ();

 ?>

 <?php

 if ($showHtml)

 {

 ?>


 <!DOCTYPE html>

 <meta charset="UTF-8">

 <html>

 <head>
 </head>

 <body>

 <form name="form1" method="POST" action="<?php echo     htmlspecialchars($_SERVER["PHP_SELF"]);?>">  


 <select id="item_select" name="month">


       <option value="">Select Month</option>
       <option value="January">January</option>
       <option value="February">February</option>
       <option value="March">March</option>
       <option value="April">April</option>
       <option value="May">May</option>
       <option value="June">June</option>
       <option value="July">July</option>
       <option value="August">August</option>
       <option value="September">September</option>
       <option value="October">October</option>
       <option value="November">November</option>
       <option value="December">December</option>

   </select>

 &nbsp;&nbsp;

 <select id="item_select" name="day">

         <option value="">Day</option>
         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
         <option value="4">4</option>
         <option value="5">5</option>
         <option value="6">6</option>
         <option value="7">7</option>
         <option value="8">8</option>
         <option value="9">9</option>
         <option value="10">10</option>

   </select>

 &nbsp;&nbsp;

 <select id="item_select" name="year">

         <option value="">Year</option>
         <option value="2015">2015</option>
         <option value="2014">2014</option>
         <option value="2013">2013</option>
         <option value="2012">2012</option>
         <option value="2011">2011</option>
         <option value="1975">1975</option>
  </select>

<br>

 <span class="error"><?php echo $monthErr;?></span>
 <span class="error"><?php echo $dayErr;?></span>
 <span class="error"><?php echo $yearErr;?></span>

 <br>

    <input type="Submit" id="submit" name="submit" value="Submit Search" style="width: 120px; color: blue;"/>

</form>

</body>

</html>

<?php

}

?>

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "xyz_database";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

}


$showHtml = true;   

$month = $day = $year = "";

$monthErr = $dayErr = $yearErr =  "";

$errorMessage = "Oops..Please correct the item(s) highlighted in red on the form below and re-submit";

function test_input($data) {  

$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);

return $data;
}


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


 // Month error & filter check code....

if (empty($_POST["month"])) {

$month = "";

} else {

$month = test_input($_POST["month"]);

if (!preg_match("/^[a-zA-Z ]*$/",$month)) {

$monthErr = "An invalid entry has been detected. Please reset this form and re-submit.";

}
}


  // Day error & filter check code....

  if (empty($_POST["day"])) {

 $day = "";

 } else {

 $day = test_input($_POST["day"]);

 if (!is_numeric($day)) {

$dayErr = "Day Found - An invalid entry has been detected. Please reset this form and re-submit.";

 }
 }

// Year error & filter check code....

if (empty($_POST["year"])) {

$year = "";

} else {

$year = test_input($_POST["year"]);

if (!is_numeric($year)) {

$yearErr = "Year Found - An invalid entry has been detected. Please reset this form and re-submit.";


}
 }


if (empty($monthErr) and empty($dayErr)  and empty($yearErr)) {


$showHtml = false;  


$value1 = $_POST['month'];
$value2 = $_POST['day'];
$value3 = $_POST['year'];


 $sql = "SELECT * FROM xyz_test_database WHERE month = ('$value1') AND day =     ('$value2') AND year = ('$value3')";

 $result = $conn->query($sql);


if ($result->num_rows > 0) {echo "<br><br><h2>Search Results</h2>
                   <table><tr>
                              <th>ID</th> 
                              <th>Time Stamp</th>
                              <th>Month</th>                                 
                              <th>Day</th>
                              <th>Year</th>
                         </tr>";

  // output data of each row

  while($row = $result->fetch_assoc()) {
     echo "<tr>
      <td>".$row["id"]."</td>
      <td>".$row["time_stamp"]."</td>
      <td>".$row["month"]."</td>
      <td>".$row["day"]."</td>
      <td>".$row["year"]."</td>
   </tr>";
 }

echo "</table>";

} else {


 echo "<p id='no_results'>Sorry - No Results Found :( </p>";

 }
 }
 }


 $conn->close();

 exit ();

 ?>

<?php

if ($showHtml)

 {

 ?>


 <!DOCTYPE html>

 <meta charset="UTF-8">

 <html>

 <head>
 </head>

 <body>

  <form name="form1" method="POST" action="<?php echo     htmlspecialchars($_SERVER["PHP_SELF"]);?>">  


 <select id="item_select" name="month">


   <option value="">Select Month</option>
   <option value="January">January</option>
   <option value="February">February</option>
   <option value="March">March</option>
   <option value="April">April</option>
   <option value="May">May</option>
   <option value="June">June</option>
   <option value="July">July</option>
   <option value="August">August</option>
   <option value="September">September</option>
   <option value="October">October</option>
   <option value="November">November</option>
   <option value="December">December</option>

  </select>

 &nbsp;&nbsp;

 <select id="item_select" name="day">

     <option value="">Day</option>
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option>
     <option value="6">6</option>
     <option value="7">7</option>
     <option value="8">8</option>
     <option value="9">9</option>
     <option value="10">10</option>

  </select>

 &nbsp;&nbsp;

  <select id="item_select" name="year">

     <option value="">Year</option>
     <option value="2015">2015</option>
     <option value="2014">2014</option>
     <option value="2013">2013</option>
     <option value="2012">2012</option>
     <option value="2011">2011</option>
     <option value="1975">1975</option>
   </select>

  <br>

  <span class="error"><?php echo $monthErr;?></span>
  <span class="error"><?php echo $dayErr;?></span>
  <span class="error"><?php echo $yearErr;?></span>

  <br>

  <input type="Submit" id="submit" name="submit" value="Submit Search" style="width: 120px; color: blue;"/>

  </form>

  </body>

  </html>

 <?php

  }

  ?>

There are a number of ways to achieve this. You can put an if statement around your html code so that it only displays if certain conditions (e.g. results aren't returned) are met.

One really simple way of doing this is to set a boolean value if results are returned. For example:

<?php

$showHtml = true;

...

if($result->num_rows > 0)
{
    $showHtml = false;
    ...

}

...

$conn->close();

if($showHtml)
{

?>

<!DOCTYPE html>

...

</html>

<?php
}
?>