Charles Murray
Charles Murray

Reputation: 383

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 process more than one item at a time with lists that have variable lengths? Currently when I click more than two boxes, only one gets processed.

Here's the HTML form:

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

and here's the simplified php:

$_POST['submitbtn'];

$completed_goal = $_POST['complete_goal'];

$query = mysql_query("UPDATE notes SET complete='1' where note_id='$completed_goal'");

Upvotes: 2

Views: 8660

Answers (4)

chrisn
chrisn

Reputation: 2125

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'] ) . ")" );

Upvotes: 3

Corubba
Corubba

Reputation: 2243

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.

Upvotes: 3

Silvertiger
Silvertiger

Reputation: 1680

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'");
}

Upvotes: 0

dyelawn
dyelawn

Reputation: 760

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

Upvotes: 0

Related Questions