【问题标题】:create 1 update button for all value为所有值创建 1 个更新按钮
【发布时间】:2016-08-15 22:22:30
【问题描述】:

我还在学习 php,我有这个脚本需要修改。在这个脚本中有多个更新按钮,我想创建另一个按钮来更新所有值。

这是我的脚本

<div class='tableContainer'>
                    <table>
                        <thead>
                            <th width='100px'>Pasaran</th>                          
                            <th width='150px'>Status</th>
                        </thead>
                        <tbody>
                            <?php foreach($pasaran as $b){ ?>
                            <tr>
                                <td width='101px'><?php echo $b['keterangan']; ?></td>
                                <td width='151px'>
                                    <form action='<?php echo base_url('home/update_pasaran'); ?>' method='POST' class='uk-form'>
                                        <input type='hidden' name='id_pasaran' value='<?php echo $b['id_pasaran']; ?>'/>
                                        <select name='status_pasaran' class='uk-form-small'>
                                            <option <?php if($b['status_pasaran']=='Offline') echo 'selected'; ?> >Offline</option>
                                            <option <?php if($b['status_pasaran']=='Online') echo 'selected'; ?> >Online</option>
                                        </select>
                                        <button class='uk-button uk-button-primary uk-button-small'>Update</button>
                                    </form>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>

我尝试添加这个脚本

<form action='<?php echo base_url('home/update_pasaran'); ?>' method='POST' class='uk-form'>
                                        <input type='hidden' name='id_pasaran' value='<?php echo $b['id_pasaran']; ?>'/>
                                        <select name='status_pasaran' class='uk-form-small'>
                                            <option <?php if($b['status_pasaran']=='Offline') echo 'selected'; ?> >Offline</option>
                                            <option <?php if($b['status_pasaran']=='Online') echo 'selected'; ?> >Online</option>
                                        </select>
                                        <button class='uk-button uk-button-primary uk-button-small'>Update</button>
                                    </form>

但是当我点击按钮时唯一更新的是最后一行。

有人可以指导我吗?

【问题讨论】:

  • 对不起我的英语不好

标签: javascript php html mysql arrays


【解决方案1】:

这就是您的代码在客户端中的样子

            <div class='tableContainer'>
                <form action='post.php' method='POST' class='uk-form'>
                    <table>
                        <thead>
                            <th width='100px'>Pasaran</th>                          
                            <th width='150px'>Status</th>
                        </thead>
                        <tbody>
                            <?php foreach($pasaran as $b){ ?>
                            <tr>
                                <td width='101px'><?php echo $b['keterangan']; ?></td>
                                <td width='151px'>
                                    <?php if($b['status_pasaran']=='Offline'){?>
                                    <td><input type="checkbox" name="Online[]" value="DB_ID"></td>
                                    <?php } else{ ?>
                                    <td><input type="checkbox" name="Offline[]" value="DB_ID"></td>
                                    <?php }?>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                    <input type="submit" value="APPLY CHANGES">
                </form>
            </div>

文件 post.php 应该是这样的:

$online_array = $_POST["Online"];
$offline_array = $_POST["Offline"];

if(isset($online_array)&&!empty($online_array)){
    foreach($online_array as $object_to_update){
        /*Your query for the data base goes here*/

    }
}

if(isset($offline_array)&&!empty($offline_array)){
    foreach($offline_array as $object_to_update){
        /*Your query for the data base goes here*/

    }

}

【讨论】:

  • 非常感谢您的回复。你能解释一下吗?为什么要使用“复选框”?
  • 复选框将出现在每个表格行中,如果选中,它将作为数组发送到服务器。
  • 我只想再创建 1 个按钮来将所有人的状态更改为“在线”或“离线”
  • 我明白了,我改变了答案,希望这符合您的需求
  • 谢谢你。我用选项更改了复选框并将表格放在表格之外,但除了最后一行的最后一个选项框外,它仍然无法正常工作......现在我有多个选项框和 1 个更新按钮。你能再指导我一次吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-14
  • 2016-01-23
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多