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')){
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗