2012-08-28 101 views
1

我将尝试尽可能地解释这一点。我从控制器返回一组结果。我希望将选定行的主键发送到另一个控制器,当用户单击“添加员工”按钮时,该控制器将管理将记录插入到数据库中。我的主要关键是MOVIE_ID。它目前正在返回两个像这样的MOVIE_ID{12341, 31351}的数组。我的问题是:将视图中的变量传递给控制器​​

  1. 如何将每个电影ID唯一地与相应的行按钮相关联?

  2. 我可以通过action=""属性将一个变量传递给控制器​​吗?我需要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

一个技巧就是通过提交按钮传递ID,像这样:

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

当点击它会发送的POST数据submit[12341]=add+employee(假设你已将所有内容都包裹在<form>标记中)。其他按钮不会被提交,只会被点击。

在PHP中,你可以检索像这样的ID:

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

的问题是,这些数据可以更改,所以我真的不能硬编码在每一个ID为它只是需要抓住它的电影按照它从控制器返回的顺序。所以我想发送第1行,如“Movie_ID [1]”或其他...如果你跟着我? – Undermine2k

+1

@ Undermine2k的权利,所以你写'... name =“submit [<?php echo $ result ['MOVIE_ID!']?>]”...' –

+0

谢谢,我会试试看。 – Undermine2k

相关问题