doqrt26664
2017-04-06 14:12
浏览 62
已采纳

如何在PHP中选择下拉菜单中的哪个选项?

I've been stuck on an assignment. In the assignment I have to use a Pull-Down Menu to change what is displayed from the database. So, for example if option A is selected then courses and grades will be shown.If on the other hand option B is selected then everything on the database will be shown. I though that if I name the options like <option value = "1" name = "Reg" >Student Registration</option> then use if (isset($_POST['Reg'])) it would work , but now I'm thinking maybe this is not the correct way to solve this issue. I'll include a snippet of the HTML code that has the menu and the full PHP code.

<?php
include("account.php");
//connect to MySQL database
session_start();
$dbc = mysqli_connect($hostname, $username, $password, $database) or die("Unable to connect to MySQL database");
echo "Connected to MySQL<br>";
$user = $_POST['name'];
$num = $_POST['number'];
if(isset($_POST['Submit']))
{
    if (empty ($user)) 
    {
        echo "you must enter your unique username <br />";
    }
    if (empty ($num)) 
    {
        echo "you must enter your ID <br />";
    }
   
    $query = "SELECT * FROM StudentDB WHERE StudentName = '". mysqli_real_escape_string($dbc,$user) ."' AND StudentID = '". mysqli_real_escape_string($dbc,$num) ."'" ;
    $result = mysqli_query($dbc,$query);
    if (mysqli_num_rows($result) == 1) 
    {
       if (isset($_POST['Reg']))
       {
           echo 'Student Name: ' . $row['Student Name'] . '| Student E-Mail: ' . $row['E-Mail'] .';'. '| Student ID: ' . $row['Student ID'] . '| Student Courses: ' . $row['Student Courses'] .'| Student Transcript: ' . $row['Student Transcript'] .'<br />';
       }
       if (isset($_POST['Tran']))
       {
           echo 'Student Name: ' . $row['Student Name'] . '| Student E-Mail: ' . $row['E-Mail'] .';'. '| Student ID: ' . $row['Student ID'] . '| Student Courses: ' . $row['Student Courses'] .'| Student Transcript: ' . $row['Student Transcript'] .'<br />';
       }
    }
    else
    {
        echo"unsuccessful login";
    }
session_destroy();
}
else
{
        echo "Empty";
}
?>
<TD>
    <select id = "myList">
    <option value = "1" name = "Reg" >Student Registration</option>
      <option value = "2" name = "Tran" >Student Transcript</option>
  </select>
</TD>

If the full HTML code is need , just ask and I'll add it. So, to summarize the question I asked : Is there a way to use if (isset($_POST[]))to check which option on the Pull-Down Menu is selected , if not then what other method can I use to do that?

Thank You

EDIT:

I changed my HTML and PHP code to :

<?php
include("account.php");
//connect to MySQL database
session_start();
$dbc = mysqli_connect($hostname, $username, $password, $database) or die("Unable to connect to MySQL database");
echo "Connected to MySQL<br>";
$user = $_POST['name'];
$num = $_POST['number'];
if(isset($_POST['Submit']))
{
    if (empty ($user)) 
    {
        echo "you must enter your unique username <br />";
    }
    if (empty ($num)) 
    {
        echo "you must enter your ID <br />";
    }
   
    $query = "SELECT * FROM StudentDB WHERE StudentName = '". mysqli_real_escape_string($dbc,$user) ."' AND StudentID = '". mysqli_real_escape_string($dbc,$num) ."'" ;
    $result = mysqli_query($dbc,$query);
    if (mysqli_num_rows($result) == 1) 
    {
       if (isset($_POST['list'] == '1'))
       {
       }
       if (isset($_POST['list'] == '2'))
       {
       } 
    }
    else
    {
        echo"unsuccessful login";
    }
session_destroy();
}
else
{
        echo "Empty";
}
?>
 <TD>
 <select id = "myList" name = "list" >
   <option value = "1" >Student Registration</option>
     <option value = "2" >Student Transcript</option>
 </select>
</TD>
This causes it to crash when I click submit. When I change the PHP code to this:

<?php
include("account.php");
//connect to MySQL database
session_start();
$dbc = mysqli_connect($hostname, $username, $password, $database) or die("Unable to connect to MySQL database");
echo "Connected to MySQL<br>";
$user = $_POST['name'];
$num = $_POST['number'];
if(isset($_POST['Submit']))
{
    if (empty ($user)) 
    {
        echo "you must enter your unique username <br />";
    }
    if (empty ($num)) 
    {
        echo "you must enter your ID <br />";
    }
   
    $query = "SELECT * FROM StudentDB WHERE StudentName = '". mysqli_real_escape_string($dbc,$user) ."' AND StudentID = '". mysqli_real_escape_string($dbc,$num) ."'" ;
    $result = mysqli_query($dbc,$query);
    if (mysqli_num_rows($result) == 1) 
    {
       /* if (isset($_POST['list'] == '1'))
       {
       }
       if (isset($_POST['list'] == '2'))
       {
       } */
    }
    else
    {
        echo"unsuccessful login";
    }
session_destroy();
}
else
{
        echo "Empty";
}
?>

it works. The change I made is just commenting out the section in-between if (mysqli_num_rows($result) == 1){}

</div>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doupaoyan6083 2017-04-06 14:21
    已采纳

    This way: Select should be named and options only have value

    <TD>
        <select id = "myList" name='listItems'>
        <option value = "1">Student Registration</option>
          <option value = "2">Student Transcript</option>
      </select>
    </TD>
    
    if(isset($_POST['listItems'] == '1')){
    
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doutian4046 2017-04-06 14:19

    You need to have an option pre-selected based on the data fetched from your database, right?

    If so, your code can't work but this should do it (not tested though):

    <select id = "myList" name="type">
        <option <?php if ($_POST['type'] == "1") { echo "selected"; } ?> value = "1" >Student Registration</option>
        <option <?php if ($_POST['type'] == "2") { echo "selected"; } ?> value = "2" >Student Transcript</option>
    </select>
    
    评论
    解决 无用
    打赏 举报
  • dsgsgs30201 2017-04-06 14:19

    Change the HTML so name is part of select instead of the options:

    <td>
        <select id="myList" name="opt_selected">
            <option value="Reg">Student Registration</option>
            <option value="Tran">Student Transcript</option>
      </select>
    </td>
    

    Then check the value of $_POST['opt_selected'] to see if it equals Reg or Tran.

    评论
    解决 无用
    打赏 举报
  • douxunzui1519 2017-04-06 14:21

    'Reg' and 'Tran' are just two possible values of the select list, which is what you're actually posting.

    Instead of (isset($_POST['Reg'])), use ($_POST['myList'] === 'Reg').

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题