Skip to content Skip to sidebar Skip to footer

Processing HTML Checkbox Forms With PHP

I am generating a checklist based on mysql data in an HTML form. When the submit button is clicked the php script changes the 'complete' field in the mysql database. How can I proc

Solution 1:

Your input element name should be an array, such as complete_goal[].

<input style="float: right;" type="checkbox" name="complete_goal[]" value="61">Milk</input>

You can update them all in a single query using a WHERE note_id IN (<?php echo implode( ',', $_POST['completed_goal'] ); ?>).

$query = mysql_query( "UPDATE `notes` SET `complete` = '1' WHERE `note_id` IN (" . implode( ',', $_POST['completed_goal'] ) . ")" );

Solution 2:

Should work and hope it helps. Afaik are all inputs with same name are treated like radiobuttons. So you have to create an array with all checkboxes.

<form method='post' action='listprocessor.php'>
  <input style="float:right" type='checkbox' name='complete_goal[]' value='61'>Milk</input>
  <input style="float:right" type='checkbox' name='complete_goal[]' value='117'>Eggs</input>
  <input style="float:right" type='checkbox' name='complete_goal[]' value='118'>Bread</input>

  <input style="float:right" type='submit' name='submitbtn' value='Completed'></input>   
</form>

<?php
  if(isset($_POST['complete_goal']))
  {
    $query = mysql_query("UPDATE `notes` SET `complete`='1' WHERE `note_id` IN ('".implode("','", array_values($complete_goal))."');");
  }
?>

EDIT: Added Chris N' idea of mysql update.


Solution 3:

I'd use a unique name attribute for each checkbox, then grab all of the checkboxes in the script you're using for submit handling and do

foreach($post_fields as $post_field_name) {
  if(isset($_POST[$post_field_name])) {
    // do your query
  }
}

Solution 4:

Perhaps this will work (might have to check syntax as it was off the top of my head). any checkbox that isnt' checked should not be listed in the $complete_goals array so you're safe to process them all :)

foreach ($_REQUEST['complete_goal'] as $key) {
    $query = mysql_query("UPDATE notes SET complete='1' where note_id='$key'");
}

Post a Comment for "Processing HTML Checkbox Forms With PHP"