Page 1 of 1

Trouble with dynamic checkboxes

Posted: Fri Mar 31, 2006 3:11 pm
by zippo
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi!

I'm having trouble figuring out why this code won't execute. I've spent a long time trying different syntax variations and ways to get around whatever the issue is, but no luck!

On Page1.php, I query mySQL to list all the rows of a certain table. In the loop that displays rows until there are none left, I have checkboxes put in before each row. At the bottom of the page I have two buttons, an Update and a Delete button. I am only working on Delete right now, and once this works I am sure I can get Update working. After a user selects one or more rows and hits the Delete button I want to display on a new page "Confirm.php" that those rows should be deleted. So I want Confirm.php to display only the rows that the user checked from Page1.php.

The trouble is, I can get the values of the checkboxes from Page1.php to display on Confirm.php only if the checkboxes are static - once I try to use the loop to generate the checkboxes I can't get the chackbox values into the Confirm.php page anymore.

Each row has a unique row_ID key. I was trying to make each checkbox "value" equal to the row_ID of the row being displayed by the loop, and the "name" of the checkbox equal to an array. I thought the checked checkboxes would be passed into the array as the row_ID's of the checked boxes. Then I would be able to loop through the array to find the row_ID's, and requery the database to get the rest of the row information... to then display them.

I think the logic works, but the code is flawed  = )


From Page1.php:

Code: Select all

//Selects all the rows to display
$result = mysql_query("SELECT * FROM db_data");

// ... some code here to generate part of HTML table

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {

// Prints out a checkbox for each displayed row
echo "<form method='post' action='Confirm.php'>
         <input type='checkbox' name='rowstore[]' value='$row[row_ID]'>
      </form>";

// ... some code here to generate part of HTML table

echo "
   <form action='Confirm.php' method='post'>
     <input type='submit' name='Delete' value='Delete' />
   <form>
     ";

From Confirm.php:

Code: Select all

// the relevant code from Confirm, checks if Delete button pressed, then:

if($_POST[Delete])
{
if(isset($_POST['rowstore']))
  {
  $rows = $_POST['rowstore'];
  $n        = count($rowstore);
  $i        = 0;

   echo "The rows you selected are: " .

   while ($i < $n)
     {
      echo "{
              $rowstore[$i]
            } ";
      $i++;
     }
  }
}

I realize this is not doing everything I want it to, but I am just trying to get it to access the values in the array 'rowstore' right now... after that I should be fine. Any suggestions on why this might not be working correctly? Is it not storing the values of the row_ID in rowstore[], or am I not passing the array correctly or somehow not accessing it correctly? I've tried doing this without isset() also, because I couldn't ever seem to get it working for some reason, but still no go. I've tried variations on these themes, but haven't gotten any of them to work, so this is pretty much my latest try...

Thank you for any help you might be able to lend me!


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Mar 31, 2006 3:22 pm
by feyd
starting and ending forms is what's shooting you in the foot. When someone clicks the delete button, none of the checkboxes will be submitted as they are in different forms.

Re: Trouble with dynamic checkboxes

Posted: Fri Mar 31, 2006 3:36 pm
by Christopher
Maybe something like:
zippo wrote:

Code: Select all

//Selects all the rows to display
$result = mysql_query("SELECT * FROM db_data");

// ... some code here to generate part of HTML table

echo '<form method='post' action="Confirm.php">';
// keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {

// Prints out a checkbox for each displayed row
    echo "<input type='checkbox' name='rowstore[$row_ID]' value='$row[$row_ID]'>";

// ... some code here to generate part of HTML table

echo "<input type='submit' name='delete[$row_ID]'' value='Delete' />";
}
echo '</form>';

Posted: Fri Mar 31, 2006 3:42 pm
by RobertGonzalez
Have you done a print_r of your $_POST array to see what, if anything, is get passed from the form?

.

Posted: Mon Apr 03, 2006 2:44 pm
by zippo
Oh good <insert favorite diety here>!

Thank you all, you were all very, very helpful. I was trying the print_r but the values were not passing correctly. The initial trouble was, as pointed out, in the construction of the form. I haven't ever tried a form like this, so the example / corrected code was also immensely helpful. I was trying for a good many hours until I finally, just now, got the correct rows to display after the query. The rest should be a snap!

And I should have known about the php code tags for posting code, I regret my transgretion - shouldn't happen again - and thanks for fixing it up for me.

Thank you again very much - this was one crazy lesson to learn. I was beginning to think I must be doing this whole array thing wrong, when you helped point out the trouble was entirely elsewhere! *Whew* what a relief = )