我会尽力解释的。我从控制器返回一组结果。我希望将所选行的主键发送到另一个控制器,当用户单击“添加员工”按钮时,该控制器将管理将记录插入数据库。我的主要密钥是 MOVIE_ID 。它目前正在返回一个包含两个的数组 电影ID {12341, 31351} 我的问题是:
MOVIE_ID
电影ID
{12341, 31351}
如何将每个电影ID与相应行的按钮唯一关联?
我可以通过将变量传递给控制器吗 action="" 属性,我需要JS吗?
action=""
<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,如下所示:
<input name="submit[12341]" type="submit" value="add employee" />
点击后,它将发送 submit[12341]=add+employee 在POST数据中(假设您已经将所有内容封装在 <form> 标签)。其他按钮不会被提交,只会被点击的按钮。
submit[12341]=add+employee
<form>
在PHP中,您可以这样检索id:
$id = key($_POST['submit']);