简体   繁体   English

更改选择选项时,再次从数据库获取数据

[英]Get data from database again, when changing select option

I have a select dropdown like this: 我有一个像这样的选择下拉列表:

<select name="city" id="order_form_select">
    <option value="" disabled selected>Ort ↓</option>
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
    <option>Option 4</option>
</select>

And I'm getting some radio buttons like this: 我得到一些像这样的单选按钮:

<?php
$statement = $pdo->prepare('SELECT some_column FROM my_table WHERE some_value = "abc" AND city = :city');
$statement->execute(array('city' => $city));
$row = $statement->rowCount();
if ($row == 0) {
   echo "<input type=\"radio\" name=\"something\" value=\"option1\">Option 1</p>";
}

$statement = $pdo->prepare('SELECT some_column FROM my_table WHERE some_value = "xyz" AND city = :city');
$statement->execute(array('city' => $city));
$row = $statement->rowCount();
if ($row == 0) {
    echo "<input type=\"radio\" name=\"something\" value=\"option2\">Option 2</p>";
}
?>

As you can see, these radio buttons are depending on the select option choice. 如您所见,这些单选按钮取决于选择选项。 That means, as soon as the user changes the selects option, the PHP code have to refresh but the select boxes choice has to stay logged in. 这意味着,只要用户更改了选择选项,PHP代码就必须刷新,但选择框选项必须保持登录状态。

What is the proper/common way to do this? 这样做的正确/常用方法是什么?

Supossing that you wanna refresh your page every time you choose a value: 假设您每次选择值时都要刷新页面:

  1. create this javascript 创建这个javascript

     <script type="text/javascript"> window.onload = function () { var citySelector = document.getElementById('order_form_select'); citySelector.addEventListener('change', function () { if (this.value) { window.location = 'yourphp.php?city=' + this.value } }, false); } </script> 
  2. In your html code, add value to your options: 在您的html代码中,为您的选项添加值:

<select name="city" id="order_form_select">
    <option value="" disabled selected>Ort ↓</option>
    <option value="City1">Option 1</option>
    <option value="City2">Option 2</option>
    <option value="City3">Option 3</option>
    <option value="City4">Option 4</option>
</select>
  1. To maintain the value after refresh: 要在刷新后保持该值:
<select name="city" id="order_form_select">
    <option value="" disabled <?php echo !isset($_GET['city']) ? 'selected' : ''; ?>>Ort ↓</option>
    <option value="City1" <?php echo isset($_GET['city']) && $_GET['city'] == 'City1' ? 'selected' : ''; ?>>Option 1</option>
    <option value="City2" <?php echo isset($_GET['city']) && $_GET['city'] == 'City2' ? 'selected' : ''; ?>>Option 2</option>
    <option value="City3" <?php echo isset($_GET['city']) && $_GET['city'] == 'City3' ? 'selected' : ''; ?>>Option 3</option>
    <option value="City4" <?php echo isset($_GET['city']) && $_GET['city'] == 'City4' ? 'selected' : ''; ?>>Option 4</option>
</select>

Edited version: 编辑版:

If you want avoid the use of javascript to do this task you can change your approach like that: 如果你想避免使用javascript来完成这个任务,你可以改变你的方法:

<form action="yourphpfile.php" method="GET">
  <select name="city" id="order_form_select">
    <option value="" disabled <?php echo !isset($_GET['city']) ? 'selected' : ''; ?>>Ort ↓</option>
    <option value="City1" <?php echo isset($_GET['city']) && $_GET['city'] == 'City1' ? 'selected' : ''; ?>>Option 1</option>
    <option value="City2" <?php echo isset($_GET['city']) && $_GET['city'] == 'City2' ? 'selected' : ''; ?>>Option 2</option>
    <option value="City3" <?php echo isset($_GET['city']) && $_GET['city'] == 'City3' ? 'selected' : ''; ?>>Option 3</option>
    <option value="City4" <?php echo isset($_GET['city']) && $_GET['city'] == 'City4' ? 'selected' : ''; ?>>Option 4</option>
    </select>
 <input type="submit" value="search by city">
</form>

Hope this helps you 希望这对你有所帮助

暂无
暂无

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

相关问题 Select 来自数据库的数据取决于节点和 ejs 中的 select 值选项 - Select data from database depends on select value option in node and ejs 如何从我使用 select 选项的数据库中获取数据,以便它可以显示在输入框中 - How to get a data from database where i using select option so it can be shown in inputbox 只有当我点击选择框时,如何从数据库中获取刷新的选项值? - How can get the refreshed option values from database only when i click on select box? 如何在选择jsp中的select标记中的选项时从数据库中检索数据 - How to retrieve data from the database when an option in a select tag in the jsp is selected 再次选择所选选项 - Select selected option again 更改选项时如何从表中动态创建的选择中获取 data-* 属性的值 - How to get the value of an data-* attribute from a dynamically created select in a table when I change an option 如何在选择选项中显示从数据库获得的价值 - How to show value which get from database in select option 如何使用Ajax使用来自数据库的数据使用select选项填充输入? - How to use ajax to populate input with select option with data from database? 使用选择选项显示来自数据库的不同数据 - show different data from database using select option 如何取消选中选项 select 上的复选框,然后从数据库中加载相应的数据并保留复选框和选项 select state - How to uncheck checkbox on option select then load corresponding data from database and persist checkbox and option select state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM