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

时间:2017-04-06 14:12:59

标签: php html mysqli

我被困在一项任务上。在分配中,我必须使用下拉菜单来更改数据库中显示的内容。因此,例如,如果选择选项A,则将显示课程和成绩。另一方面,选择选项B,然后将显示数据库上的所有内容。我虽然如果我将选项命名为<option value = "1" name = "Reg" >Student Registration</option>,那么使用if (isset($_POST['Reg']))它会起作用,但现在我想,也许这不是解决这个问题的正确方法。我将包含HTML代码片段,其中包含菜单和完整的PHP代码。

&#13;
&#13;
<?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";
}
?>
&#13;
<TD>
	<select id = "myList">
    <option value = "1" name = "Reg" >Student Registration</option>
	  <option value = "2" name = "Tran" >Student Transcript</option>
  </select>
</TD>
&#13;
&#13;
&#13;

如果需要完整的HTML代码,只需询问即可添加。那么,总结我问的问题:有没有办法使用if (isset($_POST[]))检查下拉菜单中的哪个选项被选中,如果没有,那么我可以使用哪种其他方法来做到这一点?< /强>

谢谢

编辑:

我将HTML和PHP代码更改为:

&#13;
&#13;
<?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";
}
?>
&#13;
 <TD>
 <select id = "myList" name = "list" >
   <option value = "1" >Student Registration</option>
	 <option value = "2" >Student Transcript</option>
 </select>
</TD>
&#13;
&#13;
&#13; 这会导致单击提交时崩溃。当我将PHP代码更改为:

&#13;
&#13;
<?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";
}
?>
&#13;
&#13;
&#13;

它有效。我所做的更改只是评论if (mysqli_num_rows($result) == 1){}

之间的部分

4 个答案:

答案 0 :(得分:1)

这样:选择应该命名,选项只有值

<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')){

}

答案 1 :(得分:0)

您需要根据从数据库中提取的数据预先选择一个选项,对吗?

如果是这样,您的代码无法正常工作,但应该这样做(不测试):

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

答案 2 :(得分:0)

更改HTML,因此名称是select的一部分而不是选项:

$_POST['opt_selected']

然后检查.header-title { font-size: 14px; } @media screen and (min-width: 600px) { .header-title { font-size: 16px; } } @media screen and (min-width: 1024px) { .header-title { font-size: 18px; } } 的值,看它是否等于Reg或Tran。

答案 3 :(得分:0)

'Reg'和'Tran'只是选择列表的两个可能值,这是您实际发布的内容。

而不是//URL parameters //Named route parameters are captured and added to ctx.params. router.get('/:category/:title', (ctx, next) => { console.log(ctx.params); // => { category: 'programming', title: 'how-to-node' } }); ,请使用(isset($_POST['Reg']))