将变量从视图传递到控制器
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:
How do I associate each movie ID uniquely with that corresponding rows' button?
Can I pass a variable to a controller through the
action=""
attribute , do I need 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>
我将尽力解释这个问题。 我正从控制器返回一系列结果。 我希望将所选行的主键发送到另一个控制器,该控制器将管理用户单击“添加员工”按钮时将记录插入数据库。 我的主键是 如何将每个电影ID唯一地与相应的行按钮相关联? p> li>
我可以通过 p>
MOVIE_ID code>。 它当前正在返回一个包含两个
MOVIE_ID code>
{12341,313351} code>的数组。 我的问题是: p>
action =“” code>属性将变量传递给控制器,我需要JS吗? p> li>
ol>
&lt; fieldset id =“results”&gt;
&lt;?php if(isset($ results)and count($ results)):?&gt;
&lt; table id =“box-table-a”summary =“ 员工表“&gt;
&lt; thead&gt;
&lt; tr&gt;
&lt; th scope =”col“&gt;员工&lt; / th&gt;
&lt; th scope =”col“&gt;薪资&lt; / th&gt; ;
&lt; th scope =“col”&gt; Bonus&lt; / th&gt;
&lt; th scope =“col”&gt; Supervisor&lt; / th&gt;
&lt; th scope =“col”&gt;行动&lt; / th&n;
&lt; / tr&gt;
&lt;?php foreach($ results as $ result):?&gt;
&lt; tr&gt;
//主要 key&lt;?php echo $ result ['MOVIE_ID!'];?&gt;
&lt; td&gt;&lt; span class =“Employee_name”&gt;&lt;?php echo $ result ['Employee']; ?&GT; &LT; /跨度&GT;&LT; / TD&GT;
&lt; td&gt;&lt; span class =“Salary”&gt;&lt;?php echo $ result ['Salary']; ?&GT; &LT; /跨度&GT; &lt; / td&gt;
&lt; td&gt;&lt; span class =“Bonus”&gt;&lt;?php echo $ result ['Bonus']; ?&GT; &LT; /跨度&GT; &lt; / td&gt;
&lt; td&gt;&lt; span class =“Supervisor_name”&gt;&lt;?php echo $ result ['Supervisor']; ?&GT; &lt; / span&gt;&lt; / td&gt;
&lt; td&gt;&lt; input type =“submit”value =“add employee”&gt; &lt; / td&gt;
&lt; / tr&gt;
&lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;
&lt; / thead&gt;
&lt; tbody&gt;
&lt; / fieldset&gt;
code> pre>
div>
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']);