Jump to content

[SOLVED] Limiting checkboxes?


merylvingien

Recommended Posts

Hi guys,

 

I have this code:

if(isset($_POST['selected'])) {
   foreach($_POST['selected'] as $item) {
      $sql = "SELECT * FROM tablename WHERE ID=$item";
      mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR);
      $result = mysql_query($sql);
  $num_rows = mysql_num_rows($result);
  if ($num_rows>6) {
   echo ("Too many checkboxes were selected");
   }
    else
     
     while ($db_field = mysql_fetch_assoc($result)) {

echo "stuff that is ok if number of checkboxes is correct";
}     
   } 
}

 

I am trying to limit the number of checkboxes that were selected, to say a max of 6. If the user selected any more than 6 a message should appear saying too many checkboxes were selected.

 

Can anyone see where i went wrong?

 

Cheers for any help fellas...  :D

Link to comment
https://forums.phpfreaks.com/topic/177703-solved-limiting-checkboxes/
Share on other sites

you should really do the error checking before the db query.

 

assuming $_POST['selected'] is an array of checkboxes from a form...

 

if (isset ($_POST['selected']) && (is_array ($_POST['selected'])))
{
if (count ($_POST['selected']) > 6)
{ echo ("Too many checkboxes were selected"); }
else
{
	//number of checkboxes ok .. do something;
}
}
else
{
//form not submitted OR $_POST['selected'] is not an array;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.