简体   繁体   English

如何在不使用提交按钮的情况下获得下拉选择的值?

[英]How can I get the drop down selected value without using submit button?

I want to store the dropdown selected value, and I want to pass that selected value to next page.To do this I want to store the drop down selected value with our using any button. 我想存储下拉选择的值,我想将该选择的值传递到下一页。为此,我想使用任意按钮存储下拉选择的值。 By using submit button I can do this. 通过使用提交按钮,我可以做到这一点。

My code is, 我的代码是

<?php
include_once 'Connection.php';
$sql = "select project_name from project";
$result_sql = mysqli_query($db,$sql);
echo '<form action="#" method="post">
<select name="project_name" id="project_name">';
        while ($row = mysqli_fetch_array($result_sql)) {
                echo "<option>" . $row['project_name'] ."</option>";    
            }
echo'</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>';

if(isset($_POST['submit'])){
$selected_val = $_POST['project_name'];  // Storing Selected Value In Variable
//echo "You have selected :" .$selected_val;  // Displaying Selected Value
}
?>

Since PHP is run on the server (and not on the client like JavaScript), the form has to be submitted one way or another so that your PHP code is run. 由于PHP在服务器上运行(而不是在JavaScript等客户端上运行),因此必须以一种或另一种方式提交表单,以便运行PHP代码。 You can submit a form either with a submit button, with JavaScript or by pressing enter from a text input field. 您可以使用提交按钮,JavaScript或通过在文本输入字段中按Enter来提交表单。

Depending on how you submit the form, $_POST['submit'] may not always be set. 根据提交表单的方式,可能不会总是设置$_POST['submit'] You should check if $_POST['project_name'] is set instead. 您应该检查是否设置了$_POST['project_name']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM