Jump to content

[SOLVED] Looping through checkboxes and returning ALL of them


Recommended Posts

Firstly, here's my code:

 

form.php

<tr>
  <td align="center"><input type="text" name="name_{$id}" value="{$name}" /></td>
  <td align="center"><input type="checkbox" name="showcb[]" value="{$id}" {$checked} /></td>
</tr>

 

That is in a MYSQL loop that displays all of the entries from the database (I didn't include all the code because the error isnt in that script). The checkbox allows the user to set if they want that particular entry to be shown on the homepage.

 

The collector:

post.php

<?php
// Get the post vars
foreach ($_POST['showcb'] as $key => $val)
{
// All this is doing is grabbing the post vars
$id = $val;
$name = (strlen($_POST['name_'.$id]) > 0) ? addslashes($_POST['name_'.$id]) : "";
$show = (isset($key)) ? "1" : "0";
//

$sql = "UPDATE table SET t_show='{$show}', t_name='{$name}' WHERE t_id='{$id}'";

// This echo is for debugging
echo $sql." [ $key ] [ $val ] [ {$_POST['showcb'][1]} ]<br />";

mysql_query($sql) or die(mysql_error());
}
?>

 

Now, the script works, it updates the name and and "show" of the row.

 

But here is the problem.. it only updates the rows if the checkbox is checked! So in other words, I can't un-check a checkbox because it wont get ran in the foreach() loop.

 

PHP doesn't store the checkbox in the $_POST if it isn't checked.. .so how can i go about doing this?

 

Wes

Link to comment
Share on other sites

You could pass along the number of rows returned on the form page and pass it to the post page

form.php

$rows = mysql_num_rows($result);
echo "<input type=\"hidden\" name=\"rows\" value=\"$rows\" />";

 

 

<?php
$count = $_POST['rows'];
for($i=0; $i<$count; $i++){

$name = (strlen($_POST['name_'.$id]) > 0) ? addslashes($_POST['name_'.$id]) : "";
$show = (isset($_POST['showcb'][$i])) ? "1" : "0";
//

$sql = "UPDATE table SET t_show='{$show}', t_name='{$name}' WHERE t_id='{$id}'";


mysql_query($sql) or die(mysql_error());
}
?>

 

Ray

Link to comment
Share on other sites

JavaScript is inappropriate for this kind of thing because the user may have it turned off.

 

The solution here, I think, is to change your code that renders the form.

 

<tr>
  <td align="center"><input type="text" name="rows[{$id}][name]" value="{$name}" /></td>
  <td align="center"><input type="checkbox" name="rows[{$id}][check]" value="{$id}" {$checked} /></td>
</tr>

 

Notice the subtle difference?  Now you can loop through $_POST['rows'] and each item will itself be an array with indexes name and check.

 

<?php

foreach( $_POST['rows'] as $id => $row ) {
  echo '<pre>' . print_r( $row, true ) . '</pre>';
}

?>

Link to comment
Share on other sites

JavaScript is inappropriate for this kind of thing because the user may have it turned off.

 

The solution here, I think, is to change your code that renders the form.

 

<tr>
  <td align="center"><input type="text" name="rows[{$id}][name]" value="{$name}" /></td>
  <td align="center"><input type="checkbox" name="rows[{$id}][check]" value="{$id}" {$checked} /></td>
</tr>

 

Notice the subtle difference?  Now you can loop through $_POST['rows'] and each item will itself be an array with indexes name and check.

 

<?php

foreach( $_POST['rows'] as $id => $row ) {
  echo '<pre>' . print_r( $row, true ) . '</pre>';
}

?>

 

 

Wow, nice :) I'll give it a try

Link to comment
Share on other sites

JavaScript is inappropriate for this kind of thing because the user may have it turned off.

 

The solution here, I think, is to change your code that renders the form.

 

<tr>
  <td align="center"><input type="text" name="rows[{$id}][name]" value="{$name}" /></td>
  <td align="center"><input type="checkbox" name="rows[{$id}][check]" value="{$id}" {$checked} /></td>
</tr>

 

Notice the subtle difference?  Now you can loop through $_POST['rows'] and each item will itself be an array with indexes name and check.

 

<?php

foreach( $_POST['rows'] as $id => $row ) {
  echo '<pre>' . print_r( $row, true ) . '</pre>';
}

?>

 

 

You're a freakin genious ;)

Works perfectly

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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