Converting a PHP array into an HTML array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
koosha
Forum Newbie
Posts: 5
Joined: Sun Aug 09, 2009 5:10 pm

Converting a PHP array into an HTML array

Post 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:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Converting a PHP array into an HTML array

Post by Weirdan »

There are no such things as 'HTML arrays'.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Converting a PHP array into an HTML array

Post 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?
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Converting a PHP array into an HTML array

Post by Cirdan »

Remove the brackets [ ] from the name attribute?
frao_0
Forum Commoner
Posts: 27
Joined: Sat Aug 08, 2009 3:52 am
Location: Toulouse, France

Re: Converting a PHP array into an HTML array

Post 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])!
koosha
Forum Newbie
Posts: 5
Joined: Sun Aug 09, 2009 5:10 pm

Re: Converting a PHP array into an HTML array

Post 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>';
                }
            }
 
frao_0
Forum Commoner
Posts: 27
Joined: Sat Aug 08, 2009 3:52 am
Location: Toulouse, France

Re: Converting a PHP array into an HTML array

Post by frao_0 »

hum... when on second form, kill the script with die(var_dump($_POST,1)) and see if any solutions pop up
koosha
Forum Newbie
Posts: 5
Joined: Sun Aug 09, 2009 5:10 pm

Re: Converting a PHP array into an HTML array

Post by koosha »

I got the solution. The solution is so simple: using session variables. That's it.
koosha
Forum Newbie
Posts: 5
Joined: Sun Aug 09, 2009 5:10 pm

Re: Converting a PHP array into an HTML array

Post by koosha »

I got the solution. The solution is so simple: using session variables. That's it.
koosha
Forum Newbie
Posts: 5
Joined: Sun Aug 09, 2009 5:10 pm

Re: Converting a PHP array into an HTML array

Post by koosha »

I got the solution. The solution is so simple: using session variables. That's it.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Converting a PHP array into an HTML array

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Converting a PHP array into an HTML array

Post by aceconcepts »

You don't need a session variable for something so trivial.
Post Reply