为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
米琪卡哇伊 2021-06-29 04:43:05
我有一个显示不同会议名称的下拉列表。我可以选择某个会议,但是当我选择会议时,我希望能够单击提交按钮,以便我可以获得所选会议的变量。我是数据库的新手,但我尝试添加一个表单,但我似乎无法让它在 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 connectionif (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 phpMyAdminmysqli_close($con);?><script>    $( "#searchddl" ).chosen()</script><?phpecho $db_table;?></body></html>
查看完整描述

1 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞

你没有value在你<option>的属性中给出属性,这就是$db_table当表单被 sumbitted 时没有传入任何内容的原因。而是像下面这样:


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

        }

      ?>


查看完整回答
反对 回复 2021-07-02
  • 1 回答
  • 0 关注
  • 152 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信