2

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

4 Answers 4

3

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'] ) . ")" );
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.