dqpdb82600 2019-05-21 23:16
浏览 98

下拉搜索列表无法获取等于所选选项的变量

I have this dropdown list that shows the name of different conferences. I can select a certain conference, but as I select the conference, I want to add a submit button so I can get a variable for the conference that was selected.

I'm new with databases, but I have tried adding a form, but I can't seem to get it to work inside of the php code. The database connects and shows all of the conferences just fine, I just can't figure out how to get a variable equal to the selected option.

<!DOCTYPE HTML>  
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>  

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

<script>
    $("#searchddl").chosen();
</script>

</body>
</html>

I expected a variable to equal the same thing that is selected. So I want my variable to equal whatever this is echo "<option>$row[name]</option>"; enter image description here

  • 写回答

1条回答 默认 最新

  • dongtao1262 2019-05-21 23:45
    关注
    <script>
        $( "#searchddl" ).chosen().change(function() {  // execute query every time select option changes
        var val = $( "#searchddl" ).chosen().val();  // get value from selected option
        $.ajax({
            url: "myphpfile.php", // call external PHP file for query execution
            type: "get", 
            data: { 
                myselectvalue: val // passes value as GET parameter (?=myselectvalue=val) 
            },
            success: function(response) {
                tmp = response // store variable from PHP output
                // do Something
    
            },
            error: function(xhr) {
                 // do Something else
            }
        });
    </script>
    

    In your a separate PHP file you can get the value from the get variable:

    if (isset($_GET['myselectvalue']) && strlen($_GET['myselectvalue']) { // make sure it's populated
    
        $valueFromForm = $_GET['myselectvalue'];
    
        // Execute queries, echo results, etc.
    
    }
    

    I used GET in this solution but POST would work as well. You would just need to change type: "get" to type: "post" in the ajax call and $_GET[] to $_POST[] in the PHP.

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动