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']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012 iscsi ipsec
  • ¥15 封装的 matplotlib animation 不显示图像
  • ¥15 python摄像头画面无法显示
  • ¥15 关于#3d#的问题:d标定算法(语言-python)
  • ¥15 cve,cnnvd漏洞扫描工具推荐
  • ¥15 图像超分real-esrgan网络自己训练模型遇到问题
  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)