PHP – dynamically getting variables from $_GET or $_POST

One good day I was working for building administration part of a complex Web Site. And like in other administration systems it must have the opportunity to view all users and apply some actions upon them. In my case I had a list of users and a checkbox control near them, and I had to make a delete action for selected users.

How did I find solution? Here is some piece of code.

 

Print list of users:

echo ‘<form action=”‘.$_SERVER[‘REQUEST_URI’].‘” method=”post”>’;

echo ‘<table>’;

while ( )

{

echo ‘<tr>’;

echo ‘<td><input type=”checkbox” name=”selected_user[‘.$user[‘id’].‘]” value=”1″ ></td>’;

echo ‘<td>’.$user[‘username’].‘</td>’;

echo ‘</tr>’;

}

echo ‘</table>’;

echo ‘<input type=”submit” name=”delete_user” value=”Delete”>’;

echo ‘</form>’;

 

When submit button “Delete” was activated:

if ($_POST[“delete_user”])

{

foreach($_POST[“selected_user”] as $id=>$value)

{

// delete user with userid == $id

}

}

So, at last the foreach statement gives to me “id” of selected user, and I can apply the action upon this user. It was interesting for me to find this solution of proceeding variables in PHP.

Advertisements
Posted in PHP. Tags: , , , . 1 Comment »

One Response to “PHP – dynamically getting variables from $_GET or $_POST”

  1. Idetrorce Says:

    very interesting, but I don’t agree with you
    Idetrorce


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: