如何在同一页面上显示提交后选择的下拉值


how to show selected dropdown value after submit on same page

有两个具有不同值的下拉列表和一个提交按钮。提交后,操作与$_SERVER['HP_SELF']在同一页面上;现在我想在生成报告后显示所选的下拉值,但我不知道如何做到这一点。

<form name="gg" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table align="center">
<tr>
<th>
<label>Center Name:</label>
</th>
<td>
    <select name="center_name" id="centername" required >
        <option value="">Select Center</option> 
        <option value="xxx">XXX</option>                
    </select>
</td>
</tr>
<tr>
<th>
    Age:
</th>
<td>
    <select name="age_bracket" id="agebracket" required >
        <option value=""  >Select Age</option>
        <option value="18-24"  >18-23</option>
        <option value="25-34"  >25-34</option>
        <option value="35-44"  >35-44</option>
        <option value="45-54"  >45-54</option>
        <option value="55-64"  >55-64</option>
        <option value="65-74"  >65-74</option>
        <option value="75"  >75+</option>
    </select>
</td>
</tr>
<tr>
    <td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>


<?php
//db connection goes here
echo "<table style='width:70%' table border='1' style='table-layout:fixed' align='center'>";
    echo "<tr>
    <th>No</th>
    <th>col1</th>
    <th>col2</th>
    <th>col3</th>
    <th>col4</th>
    </tr>";
if(isset($_POST['submit'])) {
//processing request here
//echo fetched rows

结果是这样的

                centername:-
                age:-
                             submit
    slno    col1    col2    col3    col4
    //after submit i get the report fetched here on the same page but could not get the selected drop-down values

我不太理解你的问题。。。我已经阅读了你的评论。。。我想你想要这样的东西吗?

<form name="gg" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table align="center">
<tr>
<th>
<label>Center Name:</label>
</th>
<td>
    <select name="center_name" id="centername" required >
        <option value="">Select Center</option> 
        <option value="xxx"<?php if(isset($_POST["center_name"]) && $_POST["center_name"] == "xxx") { echo " selected"; } ?>>XXX</option>                
    </select>
</td>
</tr>
<tr>
<th>
    Age:
</th>
<td>
    <select name="age_bracket" id="agebracket" required >
        <option value=""  >Select Age</option>
        <option value="18-24"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "18-24") { echo " selected"; } ?>>18-23</option>
        <option value="25-34"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "25-34") { echo " selected"; } ?>>25-34</option>
        <option value="35-44"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "35-44") { echo " selected"; } ?>>35-44</option>
        <option value="45-54"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "45-54") { echo " selected"; } ?>>45-54</option>
        <option value="55-64"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "55-64") { echo " selected"; } ?>>55-64</option>
        <option value="65-74"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "65-74") { echo " selected"; } ?>>65-74</option>
        <option value="75"<?php if(isset($_POST["age_bracket"]) && $_POST["age_bracket"] == "75") { echo " selected"; } ?>>75+</option>
    </select>
</td>
</tr>
<tr>
    <td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>

要获得select值,您可以简单地:

<?php
 echo $_POST['center_name'];
 echo $_POST['age_bracket'];
?>

您也可以从操作中删除<?php echo $_SERVER['PHP_SELF']; ?>

 <?php
  echo "centername:".$_POST['center_name'];
  echo "Age:" $_POST['age_bracket'];
  ?>
 <form name="gg" method="post" action="<?php echo $_SERVER['PHP_SELF'];    ?>">
 <table align="center">
 <tr>
 <th>
 <label>Center Name:</label>
 </th>
 <td>
 <select name="center_name" id="centername" required >
    <option value="">Select Center</option> 
    <option value="xxx">XXX</option>                
 </select>
 </td>
 </tr>
 <tr>
 <th>
 Age:
 </th>
 <td>
 <select name="age_bracket" id="agebracket" required >
    <option value=""  >Select Age</option>
    <option value="18-24"  >18-23</option>
    <option value="25-34"  >25-34</option>
    <option value="35-44"  >35-44</option>
    <option value="45-54"  >45-54</option>
    <option value="55-64"  >55-64</option>
    <option value="65-74"  >65-74</option>
    <option value="75"  >75+</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>
</form>