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

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型