简体   繁体   English

检查复选框是否为检查和数据传递

[英]Checking whether checkbox is check and passing of data

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: 这是setUpdateAction()和price.js的javascript:

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: 虽然我尝试检查是否选中了该框的源代码是此代码,并且它可以检查该复选框,但是现在的问题是所选数据没有传递给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... 我认为我传递数据的方式有些问题,即使它现在可以检查是否已选中复选框。

您可能会将if(isset($_POST['price']))更改为if(isset($_POST['price[]']))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM