简体   繁体   English

使用MySQL条目填充下拉列表

[英]Populating drop downs with MySQL entries

I have a MySQL database that contains, amongst other things, 2 tables. 我有一个MySQL数据库,除其他外,其中包含2个表。 One is the events table, containing event names and other details. 一个是events表,包含事件名称和其他细节。 The other is the instance table. 另一个是instance表。 This table links the events table to a venue table and adds a date, so each row is an instance of the linked event. 该表将events表链接到venue表并添加日期,因此每一行都是链接事件的实例。

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. 我现在想要做的是,所选择的情况下,抓住所有与该事件相关的实例,并填充日期另一个下拉。

Can I do this with PHP, or do I need to dip into Javascript? 我可以使用PHP来做到这一点,还是需要使用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. 我想我只是需要一些方法来抓住查询基于该下拉选择的event_id值,然后,但我不知道怎么没有JavaScript。

You should be looking at Javascript or jQuery for achieving your goal. 您应该使用Javascript或jQuery实现目标。 I've used jQuery based on my question to you earlier. 我之前根据您的问题使用过jQuery。 It's also simpler and less code. 它也更简单,代码更少。

Your PHP: 您的PHP:

Add an ID attribute event_menu to your select menu 将ID属性event_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>

Using jQuery: 使用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!");
        }
    });
});

On populate-menu.php , have something like: 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>';

This new menu will then be posted back to your original page into the container_for_new_menu element. 然后,这个新菜单将被放回到container_for_new_menu元素的原始页面中。

By the looks of it, you want to populate the "instances" dropdown based on the selection the user makes on the "event" dropdown. 从外观上看,您想根据用户在“事件”下拉列表中所做的选择填充“实例”下拉列表。 You cannot do this without Javascript. 没有Javascript,您将无法做到这一点。

My suggested way of doing this is to use AJAX to pull the instance data and populate the "instances" dropdown on change of the "event" dropdown. 我建议的方法是使用AJAX提取实例数据,并在“事件”下拉列表更改时填充“实例”下拉列表。 Useful resources below for simple AJAX get with jQuery: 以下简单的AJAX的有用资源可通过jQuery获得:

http://api.jquery.com/jQuery.get/ http://api.jquery.com/jQuery.get/

http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/ http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/

You need some kind of Javascript to accomplish this. 您需要某种Javascript才能完成此任务。 Either: Basic- submit the form on select and let php populate the instance drop-down. 可以:基本-在select上提交表单,然后让php填充实例下拉列表。 More elegant- use Javascript to make an Ajax call on select which will dynamically replace the instance drop-down's div. 更优雅-使用Javascript在select上进行Ajax调用,它将动态替换实例下拉列表的div。

You will need JavaScript to populate the second drop down box. 您将需要JavaScript来填充第二个下拉框。 I suggest you load all the values into JSON on the page and then you can just use a jQuery on change event to populate the second select box. 我建议您将所有值都加载到页面上的JSON中,然后可以使用jQuery on change事件填充第二个选择框。

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

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