therealwesfoster Posted May 12, 2008 Share Posted May 12, 2008 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 Quote Link to comment Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 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 Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted May 12, 2008 Author Share Posted May 12, 2008 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 Quote Link to comment Share on other sites More sharing options...
benphp Posted May 12, 2008 Share Posted May 12, 2008 How about using a hidden field and use Javascript to populate the hidden field. <input type="hidden" name="update" value="yes"> <input type="hidden" name="update" value="no"> etc. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 12, 2008 Share Posted May 12, 2008 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted May 12, 2008 Author Share Posted May 12, 2008 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 Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted May 14, 2008 Author Share Posted May 14, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.