使用MySQL条目填充下拉列表 [英] Populating drop downs with MySQL entries
问题描述
我有一个MySQL数据库,其中包含2个表。一个是事件
表,包含事件名称和其他详细信息。另一个是实例
表。此表将事件
表链接到场所
表并添加日期,因此每行都是链接的实例活动。
I have a MySQL database that contains, amongst other things, 2 tables. One is the events
table, containing event names and other details. The other is the instance
table. This table links the events
table to a venue
table and adds a date, so each row is an instance of the linked event.
我正在制作一个活动预订表格,供这些活动内部使用。我想允许通过下拉列表选择要预订的活动。所以,我已经用事件名称填充了一个下拉列表:
I am making an event booking form for internal use for these events. I want to allow selection of the event to be booked via a dropdown list. So, I have populated one dropdown with the event names:
$qEvent = "SELECT event_name, event_id FROM events";
$rEvent = mysqli_query($dbc,$qEvent);
echo '<select>';
while ($row = mysqli_fetch_assoc($rEvent)) {
echo '<option value="'.$row['event_id'].'">'.$row['event_name'].'</option>';
}
echo '</select>';
我现在想要做的是,对于所选事件,抓住与该事件相关的所有实例,并使用日期填充另一个下拉列表。
What I now want to do is, for the selected event, grab all the instances associated with that event, and populate another dropdown with the dates.
我可以使用PHP执行此操作,还是需要深入了解Javascript?我想我只需要一些方法来获取下拉选择的event_id值,然后根据它进行查询,但我不知道如何没有Javascript。
Can I do this with PHP, or do I need to dip into Javascript? I think I just need some way to grab the event_id value of the dropdown selection and then query based on that, but I don't know how without Javascript.
推荐答案
你应该看看Javascript或jQuery来实现你的目标。我之前已经根据我的问题使用了jQuery。它也更简单,代码更少。
You should be looking at Javascript or jQuery for achieving your goal. I've used jQuery based on my question to you earlier. It's also simpler and less code.
您的PHP:
添加ID属性 event_menu
到你的选择菜单
Add an ID attribute event_menu
to your select menu
echo '<select id="event_menu">';
while ($row = mysqli_fetch_assoc($rEvent)) {
echo '<option value="'.$row['event_id'].'">'.$row['event_name'].'</option>';
}
echo '</select>';
<div id="container_for_new_menu"></div>
使用jQuery:
$('#event_menu').on('change', function() {
// get selected value and build data string for AJAX
var event_selected = "event_selected="+$(this).val();
// send the selected data to a PHP page to build the populated menu
$.ajax({
url : 'populate-menu.php',
type: 'POST',
data : event_selected,
dataType : 'html',
success : function(data) {
$('#container_for_new_menu').html(data);
}, error : function() {
alert("Something went wrong!");
}
});
});
在 populate-menu.php 上,有类似的内容:
$event_selected = isset($_POST['event_selected']) ? $_POST['event_selected'] : null;
// do SQL query here based on user's selection
// making sure you validate the data in the POST request for malicious BS
// or use parameterized queries
// then build a new menu to send back
echo '<select>';
// loop through results and build options
echo '</select>';
这个新菜单将被发回原始页面,进入 container_for_new_menu
元素。
This new menu will then be posted back to your original page into the container_for_new_menu
element.
这篇关于使用MySQL条目填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!