下拉搜索列表需要有一个带有提交按钮的 html 表单

我有一个显示不同会议名称的下拉列表。我可以选择某个会议,但是当我选择会议时,我希望能够单击提交按钮,以便我可以获得所选会议的变量。


我是数据库的新手,但我尝试添加一个表单,但我似乎无法让它在 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>


米琪卡哇伊
浏览 153回答 1
1回答

慕运维8079593

你没有value在你<option>的属性中给出属性,这就是$db_table当表单被 sumbitted 时没有传入任何内容的原因。而是像下面这样:&nbsp; <?php&nbsp; &nbsp; //Declare variables&nbsp; &nbsp; $db_host = "";&nbsp; &nbsp; $db_username = "";&nbsp; &nbsp; $db_pass = "";&nbsp; &nbsp; $db_name = "";&nbsp; &nbsp; //$db_table = "";&nbsp; &nbsp; //Connect to phpMyAdmin&nbsp; &nbsp; $con=mysqli_connect("$db_host","$db_username","$db_pass","$db_name");&nbsp; &nbsp; // Check connection&nbsp; &nbsp; if (mysqli_connect_errno())&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; echo "Failed to connect to MySQL: " . mysqli_connect_error();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; mysqli_select_db($con,"$db_name") or die ("No database");&nbsp; &nbsp; ?>&nbsp; &nbsp; <form action="" name="selection" method="post">&nbsp; &nbsp; <select project="ConferenceList" id="ConferenceList" name="ConferenceList">&nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; $result=mysqli_query($con,"select * From conferenceList");&nbsp; &nbsp; echo "<option> -- Search Conference Name -- </option>";&nbsp; &nbsp; while($row=mysqli_fetch_array($result))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; //when form will get submitted whatever will be in value get passed&nbsp; &nbsp; &nbsp; &nbsp; echo "<option value='.$row[name].'>$row[name]</option>";&nbsp; &nbsp; }&nbsp; &nbsp; echo "</select>";&nbsp; &nbsp; //Close phpMyAdmin&nbsp; &nbsp; mysqli_close($con); ?>&nbsp; &nbsp; <input type="submit" name="submit" id="submit" value="Submit" />&nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp;<?php&nbsp; &nbsp; &nbsp; &nbsp; $db_table = "";&nbsp; &nbsp; &nbsp;//checking if form is submit&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (isset($_POST["submit"])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $db_table = $_POST["ConferenceList"];//will give you value of option selected&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $db_table;//printing value&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ?>
打开App,查看更多内容
随时随地看视频慕课网APP