我有这个下拉列表,显示了不同会议的名称。我可以选择某个会议,但是在选择会议时,我希望能够单击“提交”按钮,以便可以获取所选会议的变量。
我是数据库的新手,但是我尝试添加表单,但是似乎无法在PHP代码中使用它。数据库连接并显示所有会议都很好,我只是想不出如何获取等于所选选项的变量。表单已提交,但我没有任何价值。我在网上浏览了所有内容,却一无所获。
.error {
color: #FF0000;
}
<!DOCTYPE HTML>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css" />
</head>
<body>
<form action="" name="selection" method="post">
<select project="ConferenceList" id="ConferenceList" name="ConferenceList">
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
<?php
//Declare variables
$db_host = "";
$db_username = "";
$db_pass = "";
$db_name = "";
//$db_table = "";
//Connect to phpMyAdmin
$con=mysqli_connect("$db_host","$db_username","$db_pass","$db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"$db_name") or die ("No database");
$result=mysqli_query($con,"select * From conferenceList");
echo "<select id='searchddl'>";
echo "<option> -- Search Conference Name -- </option>";
while($row=mysqli_fetch_array($result))
{
echo "<option>$row[name]</option>";
}
echo "</select>";
//Close phpMyAdmin
mysqli_close($con);
?>
<script>
$( "#searchddl" ).chosen()
</script>
<?php
echo $db_table;
?>
</body>
</html>
答案 0 :(得分:1)
您未在value
中提供<option>
属性,这就是在汇总表格时$db_table
中没有传递任何内容的原因。而是像下面这样:
<?php
//Declare variables
$db_host = "";
$db_username = "";
$db_pass = "";
$db_name = "";
//$db_table = "";
//Connect to phpMyAdmin
$con=mysqli_connect("$db_host","$db_username","$db_pass","$db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"$db_name") or die ("No database");
?>
<form action="" name="selection" method="post">
<select project="ConferenceList" id="ConferenceList" name="ConferenceList">
<?php
$result=mysqli_query($con,"select * From conferenceList");
echo "<option> -- Search Conference Name -- </option>";
while($row=mysqli_fetch_array($result))
{
//when form will get submitted whatever will be in value get passed
echo "<option value='.$row[name].'>$row[name]</option>";
}
echo "</select>";
//Close phpMyAdmin
mysqli_close($con); ?>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
<?php
$db_table = "";
//checking if form is submit
if (isset($_POST["submit"])) {
$db_table = $_POST["ConferenceList"];//will give you value of option selected
echo $db_table;//printing value
}
?>