根据下拉菜单选择

时间:2016-06-08 12:59:41

标签: php database variables drop-down-menu

我有两个表:event_title列出了Event_title_ID,标题和即将发生的事件的日期,event_reg存储了提交表单中的数据。

我正在使用WHILE循环从event_title表中获取数据以填充下拉菜单:

<select id="title" name="Event_title_ID">
<option value="0">Please Select One</option>
<?php
$q = "SELECT Event_title_ID, title, date FROM event_title ORDER BY date ASC";
    $r = mysqli_query($connection, $q);
    if (mysqli_num_rows($r) > 0) {
        while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) 
        {
            echo '<option value="' . $row['Event_title_ID'] . '">' . $row['title'] . '</option>';
        }
    } else {
        echo '<option>No event to register for at this time</option>';
    }
?>
</select>

运行验证后,我将$ row [&#39; Event_title_ID&#39;]设置为$ Event_title_ID并将其插入event_reg表。

根据用户选择的下拉项,我还需要将$row['title']存储为变量$title,以便可以在发送到的电子邮件中调用它管理员。

我不确定如何根据用户从下拉菜单中的值中选择的内容来存储第二个值。

**** **** UPDATE

感谢@shut的答案。这是我的代码:

表格形式:

<select id="title" name="Event_title_ID">
<option value="0">Please Select One</option>

<?php // retrieve all the events from the event_title table
$q = "SELECT Event_title_ID, title, date FROM event_title ORDER BY date ASC";
$r = mysqli_query($connection, $q);

if (mysqli_num_rows($r) > 0) {
    while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC))
    {
        echo '<option value="' . $row['Event_title_ID'] .'-'.$row['title']. '">' . $row['title'] . '</option>';
    }
} else {
    echo '<option>No event to register for at this time</option>';
}
?>
</select>

在验证中:

<?php
    $errors = array();
    if (isset($_POST['Event_title_ID']) && $_POST['Event_title_ID'] == '0') 
        {
            $errors[] = '<p id="eventErr">Please select an event.</p>';
        } else {
            $event = explode('-' , $_POST['Event_title_ID']);
            $Event_title_ID = $event[0];
            $title = $event[1];
        }
?>

1 个答案:

答案 0 :(得分:1)

您可以对select选项值使用event_title和event_title_id,并在验证后展开该值。 示例

 echo '<option value="' . $row['Event_title_ID'] .'-'.$row['title']. '">' . $row['title'] . '</option>';

并在验证后使用

$event = explode('-' , $_POST['Event_title_ID']);
$Event_title_ID=$event[0];
$title=$event[1];