简体   繁体   中英

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:

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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