繁体   English   中英

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

[英]html form using PHP_SELF & php validation - after submit, results displayed on new page without displaying form

我正在尝试使用下面发布的类似代码创建html搜索表单。

提交表单时,我想提交给PHP_SELF

我想使用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

}

?>

有多种方法可以实现此目的。 您可以在HTML代码周围放置一个if语句,以便仅在满足某些条件(例如不返回结果)时显示。

一种非常简单的方法是,如果返回结果,则设置一个布尔值。 例如:

<?php

$showHtml = true;

...

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

}

...

$conn->close();

if($showHtml)
{

?>

<!DOCTYPE html>

...

</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

  }

  ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM