douwen4178 2012-08-28 06:42
浏览 91
已采纳

将变量从视图传递到控制器

I'm going to try to explain this as best I can. I'm returning an array of results from my controller. I wish to send over the primary key of the selected row to another controller that will manage inserting the record into the Database when a user clicks the "add employee" button. My primary key is MOVIE_ID. It's currently returning an array of two MOVIE_ID {12341, 31351} like so. My questions are:

  1. How do I associate each movie ID uniquely with that corresponding rows' button?

  2. Can I pass a variable to a controller through the action="" attribute , do I need JS?

enter image description here

            <fieldset id= "results">

                <?php if (isset($results ) And count($results)) : ?>
                    <table id="box-table-a" summary="Employee Sheet">
    <thead>
        <tr>
            <th scope="col">Employee</th>
            <th scope="col">Salary</th>
            <th scope="col">Bonus</th>
            <th scope="col">Supervisor</th>
            <th scope="col">Action</th>
        </tr>


            <?php foreach ($results as $result) : ?>
                            <tr>
//Primary key <?php echo $result['MOVIE_ID!'];?>
<td><span class="Employee_name"><?php echo $result['Employee']; ?> </span></td>         
<td><span class="Salary"><?php echo $result['Salary']; ?> </span> </td>
<td><span class="Bonus"><?php echo $result['Bonus']; ?> </span> </td>
<td><span class="Supervisor_name"><?php echo $result['Supervisor']; ?> </span></td>
<td><input type="submit" value="add employee" > </td>

    </tr>

    <?php endforeach; ?>
        <?php endif; ?>
    </thead>
        <tbody>
    </fieldset>
  • 写回答

1条回答 默认 最新

  • dongshuo5101 2012-08-28 06:47
    关注

    One trick is to pass the id via the submit button, like so:

    <input name="submit[12341]" type="submit" value="add employee" />
    

    When clicked it would send submit[12341]=add+employee in the POST data (assuming you have wrapped everything inside a <form> tag). The other buttons will not be submitted, only the one that's clicked.

    In PHP you can retrieve the id like so:

    $id = key($_POST['submit']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改