简体   繁体   English

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

[英]passing variables from views to controllers

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 . 我的主键是MOVIE_ID It's currently returning an array of two MOVIE_ID {12341, 31351} like so. 像这样{12341, 31351}当前正在返回两个MOVIE_ID {12341, 31351}的数组。 My questions are: 我的问题是:

  1. How do I associate each movie ID uniquely with that corresponding rows' button? 如何将每个电影ID与相应行的按钮唯一关联?

  2. Can I pass a variable to a controller through the action="" attribute , do I need JS? 我可以通过action=""属性将变量传递给控制器​​吗,我需要JS吗?

在此处输入图片说明

            <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>

One trick is to pass the id via the submit button, like so: 一种技巧是通过提交按钮传递id,如下所示:

<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). 单击后,它将在POST数据中发送submit[12341]=add+employee (假设您已将所有内容包装在<form>标记内)。 The other buttons will not be submitted, only the one that's clicked. 其他按钮将不会提交,只有单击的按钮会提交。

In PHP you can retrieve the id like so: 在PHP中,您可以像这样检索ID:

$id = key($_POST['submit']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM