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

时间:2015-02-06 08:38:46

标签: php html

有两个具有不同值的下拉列表和一个提交按钮。提交后,该操作与$ _SERVER [' PHP_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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我不太了解你的问题......我已经阅读了你的意见......我想你想要这样的事情?

<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']; ?>