根据选择提交表单


Submit Form to action based on Selection

我有一个表中的记录列表,每条记录旁边都有一个复选框。

用户通过选中复选框来选择一些记录,然后从链接的下拉列表中选择一个操作。

<div class="button-group">
    <button type="button" id="but1">Action</button>
    <ul class="dropdown" id="dropdown-but1">
        <li><a href="#">Update Attendees</a></li>
        <li><a href="#">Another action</a></li>
        <li><a href="#">One more action</a></li>
    </ul>
</div>

一个动作的例子可能是当用户参加一个活动时进行更新。

if(isset($_POST['submit']))
{
    foreach ($_POST['checkbox_mark'] as $value => $dummy) {
        $option = isset($_POST['checkbox'][$value]) ? '1' : '0';
        // Send the Data to the Model
        $eventRegistrationModel->markAttended($value, $id, $option);
    } 
}

在分钟我有这个工作为一个单一的提交按钮,但我希望用户能够从一个下拉菜单的选项列表中进行选择,然后调用适当的操作。

我似乎找不到这样的例子,但我可能不是在搜索正确的术语。假设我需要使用jquery。

我已经找到了一种方法来做到这一点使用表单元素https://stackoverflow.com/a/17423522/1472203但想知道是否可以与文本链接。

让提交按钮看起来像链接

然后为每个操作设置尽可能多的提交按钮,并在操作文件中检查按钮的提交情况:

<div class="button-group">
    <button type="button" id="but1">Action</button>
    <ul class="dropdown" id="dropdown-but1">
        <li><input type="submit" name="update_attendees" value="Update Attendees" class="buttonThatLooksLikeALink"></li>
        <li><input type="submit" name="another_action" value="Another action" class="buttonThatLooksLikeALink"></li>
        <li><input type="submit" name="one_more_action" value="One more action" class="buttonThatLooksLikeALink"></li>
    </ul>
</div>

然后在PHP中:

<?php
if($_SERVER["REQUEST_METHOD"]==="POST")
{
    if(isset($_POST["update_attendees"]))
    {
        # Update those attendees!
    }
    elseif(isset($_POST["another_action"]))
    {
        # Do that other action!
    }
    elseif(isset($_POST["one_more_action"]))
    {
        # Do the OTHER action
    }
}