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

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

Thanks for the suggestion :)

 

But since rows can be deleted, I can't rely on the row's ID to be between 1-50 you know? Since it's all auto-increment

 

Have an idea around only updating the row if it exists? This is probably a basic question

 

Wes

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

?>

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

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

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.