douhuan1950 2018-01-29 07:34
浏览 61

检查复选框是否已检查并传递数据

I'm trying to check whether the checkbox have been checked before it can proceed to update the data of the box that have been checked.

This is my original code and it works fine but it is unable to check whether a checkbox have been checked:

echo'<form name = "frmPrice" method="post" action="">';
    echo'<table align="center">
          <tr>
             <th></th>
             <th>Types</th>
             <th>Price</th>
          </tr>';
          while($row = mysql_fetch_assoc($result))
          {
            echo'<tr>';
            echo'<td><input type="checkbox" name="price[]" 
                  value='.$row['price_id'].'</td>';
            echo'<td>'.$row['price_type'].'</td>';
            echo'<td>'.$row['price_perunit'].'</td>';
            echo'</tr>';
           }    
   echo'</table>';
   echo'<br><br><input type="button" name="update" value="Update" 
          onClick="setUpdateAction();"/>';
echo'</form>';

This one is the javascript for setUpdateAction(), price.js:

function setUpdateAction() {
document.frmPrice.action = "update.php";
document.frmPrice.submit();
}

While the source code that I try to check for whether the box is checked or not is this and it can check for the checkbox but the problem now is that the data that is selected does not pass to the update.php:

echo'<form name = "frmPrice" method="post" action="">';
        echo'<table align="center">
              <tr>
                 <th></th>
                 <th>Types</th>
                 <th>Price</th>
              </tr>';
              while($row = mysql_fetch_assoc($result))
              {
                echo'<tr>';
                echo'<td><input type="checkbox" name="price[]" 
                      value='.$row['price_id'].'</td>';
                echo'<td>'.$row['price_type'].'</td>';
                echo'<td>'.$row['price_perunit'].'</td>';
                echo'</tr>';
               }    
       echo'</table>';
       echo'<br><br><input type="submit" name="update" value="Update"/>';
echo'</form>';

if(isset($_POST['update']))
{
   if(isset($_POST['price']))
   {
     echo "<script language='javascript' type='text/javascript'>";
     echo'function setUpdateAction() {
     document.frmPrice.action = "update.php";
     document.frmPrice.submit();}';
     echo "</script>";
   }
   else
   {
     echo "<script language='javascript' type='text/javascript'>";
     echo "alert('You didn't select any data.')";
     echo "</script>";
    }
}

I think there is something wrong in my way of passing the data even though it now can check for whether a checkbox have been checked...

  • 写回答

1条回答 默认 最新

  • douchao5864 2018-01-29 07:47
    关注

    Yuo may to change if(isset($_POST['price'])) to if(isset($_POST['price[]']))

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算