duankua3620 2012-09-19 10:17
浏览 16
已采纳

请帮我调试这个PHP代码

Morning All,

I have a form that I am using to add multiple rows into a database at once. It all seems to be working fine, except for the checkboxes. There is one checkbox for each form row. Regardless of what order the checkboxes are ticked, the results are always returned in the same order after being submitted.

Here is the main part of the form that matters:

<tr>
<td class='border'>
<input type='text' name='rowid[]' value='".$row."' style='display:none;' />
<input type='text' name='ref[]' style='width:100%;' />
</td>
<td class='border'>
<input type='text' name='obs[]' style='width:100%;' />
</td>
<td class='border'>
<input type='text' name='act[]' style='width:100%;' />
</td>
<td class='border' align='center'>
<input type='checkbox' name='comp[]' value='Yes' />
</td>
<td class='border'>
<input type='text' name='compby[]' style='width:100%;' />
</td>
<td class='border'>
<input type='text' name='compdate[]' id='completeddate".$row."' class='completeddate' style='width:100%;' />
</td>
</tr>

This form gets submitted, and my PHP code deals with it. You can see that the very first input, is the ID of the row of the form. This is a hidden field and the value is incremented by one for each row that is displayed in the form.

Once the form is submitted, I use the below PHP to get deal with each row, one at a time

foreach($_POST['rowid'] as $key=>$rowid) {

This should basically say that for a rowid, the rest of the inputs are associated, until it gets to the next rowid.

If I were to then print say

echo $_POST['ref'][$key];

I will get the reference for the first row, and then once the loop completes and prints it again, I will get the reference for the second row, and so on.

However, if I were to put

echo $_POST['comp'][$key];

Then this is where it breaks.

Let's say for example, that for each row I ticked the checkboxes in this order:

row 1, tick, row 2, untick, row 3, tick

If I then let the loop print out all of the checkbox values, it will display

'Yes', 'Yes', ''

But it should be in this order

'Yes', '', 'Yes'

Is this because the checkbox is normal an array anyway, and I am then putting this into another array by giving it a name of comp[]?

Is there anyway around this issue, or would I be better off having radio buttons, one for yes, and one for no?

Many thanks

Eds

  • 写回答

2条回答 默认 最新

  • doujiling4377 2012-09-19 10:37
    关注

    The proper way to create a technical hidden field in a form is to use the hidden type. Anyway, you don't need this field here. Instead, use $row in all fields to properly identify them:

    <tr>
    <td class='border'>
    <input type='text' name='ref[<?php echo $row; ?>]' style='width:100%;' />
    </td>
    <td class='border'>
    <input type='text' name='obs[<?php echo $row; ?>]' style='width:100%;' />
    </td>
    <td class='border'>
    <input type='text' name='act[<?php echo $row; ?>]' style='width:100%;' />
    </td>
    <td class='border' align='center'>
    <input type='checkbox' name='comp[<?php echo $row; ?>]' value='Yes' />
    </td>
    <td class='border'>
    <input type='text' name='compby[<?php echo $row; ?>]' style='width:100%;' />
    </td>
    <td class='border'>
    <input type='text' name='compdate[<?php echo $row; ?>]' id='completeddate<?php echo $row; ?>' class='completeddate' style='width:100%;' />
    </td>
    </tr>
    

    Change the loop (the rowid field no longer exists). You may loop on any of your text fields, since they will be submitted even if empty (contrary to checkboxes fields, that are submitted only when checked):

    foreach($_POST['ref'] as $key => $rowid) {
    

    Check your PHP code, there may be more things to update.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集