Page 1 of 1

Converting a PHP array into an HTML array

Posted: Sun Aug 09, 2009 5:11 pm
by koosha
I'm writing a PHP program. I've encountered a problem; in the following code I try to pass $_POST['delete'] which is an array as the value of a hidden input to some form, but it doesn't do so.

In fact, there's something wrong with converting PHP array into HTML array.

I'm sure that $_POST['delete'] is not null and is a real array.

echo '<input type="hidden" name="delete[]" value="'.$_POST['delete'].'" />';

Please help me! :banghead:

Re: Converting a PHP array into an HTML array

Posted: Sun Aug 09, 2009 6:50 pm
by Weirdan
There are no such things as 'HTML arrays'.

Re: Converting a PHP array into an HTML array

Posted: Sun Aug 09, 2009 7:45 pm
by aceconcepts
What are you trying to do exactly - overall? Don't say you're trying to create an HTML array. Like Weirdan said they don't exist.

What's your objective?

Re: Converting a PHP array into an HTML array

Posted: Sun Aug 09, 2009 11:42 pm
by Cirdan
Remove the brackets [ ] from the name attribute?

Re: Converting a PHP array into an HTML array

Posted: Mon Aug 10, 2009 3:15 am
by frao_0
I agreed with Cirdan, you don't need the brackets here. If you use the bracket, then the $_POST will be an array (i.e., echo $_POST['delete'][0])!

Re: Converting a PHP array into an HTML array

Posted: Mon Aug 10, 2009 4:51 am
by koosha
I'm trying to prompt user to confirm the deletion of some values selected by checkboxes. There is a first form whose checkboxes' name is 'delete[]'(that must include brackets so that it can be treated as an array of values). Then that array is passed to a second form as a hidden input. The second form including a question message and a couple of buttons(Yes & No) uses the 'delete' array in order to remove the desired values from DB, if the user clicks 'Yes'.
The behavior of that is much like of phpMyAdmin's, when trying to delete some rows of a table.

Here's the code:

Code: Select all

 
/* Proccessing the Second Form Result */
            if (isset($_POST['sure_submit'])) {
                if (isset($_POST['delete'])) {
                    foreach ($_POST['delete'] as $user) {
                      $query = "DELETE FROM users_tbl WHERE username='$user'";
                      mysql_query($query);
                    }
                }
            }
/* The Second Form */
            if (isset($_POST['submit'])) {
                if (isset($_POST['delete'])) {
                    echo '<form action="'.$self.'" method="POST">';
                    echo 'Are you sure you want to delete the following user(s): <br />';
                        foreach ($_POST['delete'] as $user)
                          echo "$user<br />";
                    echo '<input type="submit" name="sure_submit" value=" Yes " />';
                    echo '<input type="submit" name="no" value=" No " />';
                    echo '<input type="hidden" name="delete[]" value="'.$_POST['delete'].'" />';
                    echo '</form>';
                }
            }
 

Re: Converting a PHP array into an HTML array

Posted: Mon Aug 10, 2009 1:18 pm
by frao_0
hum... when on second form, kill the script with die(var_dump($_POST,1)) and see if any solutions pop up

Re: Converting a PHP array into an HTML array

Posted: Mon Aug 10, 2009 5:31 pm
by koosha
I got the solution. The solution is so simple: using session variables. That's it.

Re: Converting a PHP array into an HTML array

Posted: Mon Aug 10, 2009 5:31 pm
by koosha
I got the solution. The solution is so simple: using session variables. That's it.

Re: Converting a PHP array into an HTML array

Posted: Mon Aug 10, 2009 5:31 pm
by koosha
I got the solution. The solution is so simple: using session variables. That's it.

Re: Converting a PHP array into an HTML array

Posted: Mon Aug 10, 2009 5:35 pm
by aceconcepts
From your second form remove the square brackets from your delete element:

Code: Select all

 
//remove the square brackets as the value you are using is already an array
//change
echo '<input type="hidden" name="delete[]" value="'.$_POST['delete'].'" />';
 
//to
echo '<input type="hidden" name="delete" value="'.$_POST['delete'].'" />';
 
I don't know what your first form looks like but this should work.

Re: Converting a PHP array into an HTML array

Posted: Mon Aug 10, 2009 5:36 pm
by aceconcepts
You don't need a session variable for something so trivial.