Quoting through $ _POST variables

Sorry, I could not find a suitable name for this question. I created the following using a for loop, and I linked the names of the submit buttons using the template below: submit_edit_category_1 submit_edit_category_2 submit_edit_category_3

echo "<input type='submit' value = 'Edit' name='submit_edit_category_" . $obj_categories_admin->categories[$i]['category_id'] . "'/>"; 

I want to skip these values ​​so that I can use the action of the button whichis edit_category and the category identifier, which is 1.2 or 3. I want something like:

 if(isset($_POST) == 'edit_category')) { //code here } 

Someone suggested I do this as follows:

 name="submit[which_action][which_category]" a1 = $_POST['submit']; $which_action = reset(array_keys($a1)); $which_category = reset(array_keys($a1[$which_action])); 

This does not work. Can someone give me another way to do this? Thanks!

+6
php mysql
source share
4 answers

here is what i will do:

for the actual form, I would use array keys to link the action and the corresponding identifier information.

 $cat_id = $obj_categories_admin->categories[$i]['category_id']; echo "<input type='submit' value = 'Edit' name='submit[edit_category][" . $cat_id . "]'/>"; 

and then when sent, I can do:

 <?php list($action, $action_params) = each($_POST['submit']); list($cat_id, $button_label) = each($action_params); print_r($_POST['submit']); // prints array('edit_category' => array('1' => 'Edit')) echo($action); //prints "edit_category" print_r($action_params); //prints array('1' => 'Edit') echo($cat_id); //prints "1" echo($button_label); //prints "Edit" 

edit: for more information about each (), go here: http://us2.php.net/each . I personally always felt that the lack of distinction between the button label and its meaning is frustrating. Using the array key to populate the information in this button has always been my favorite hack.

+17
source share

UPD: Please use Mike's advice. It is much better to have more structured data in POST.

 foreach($_POST as $key => $val) { if(strpos($key, 'submit_edit_category_') === 0 ) { print $key.' => '.$val.'\r\n'; print substr($key, 21 /* or 22... or 23... try yourself */ ); } } 
+24
source share

You can try the following:

 foreach ($_POST AS $key=>$value) { if (strpos($key, 'submit_edit_category_') !== false) { $catID = (int)str_replace('submit_edit_category_', '', $key); echo 'Category ID: ' . $catID . '<br />'; } } 
+2
source share

I would change the way I create a name for this:

submit__edit_category__1

Then try the following:

 function filter_by_submit($var) { return stripos($var, "submit") !== false ? true : false; } $submits = array_filter(array_keys($_POST), "filter_by_submit"); foreach ($submits as $sub) { if ($_POST[$sub] == "Edit") { list($submit, $action, $id) = explode("__", $sub); break; } } 

$submit will contain the string "submit" . $action will contain "edit_category" , and $id will contain the identifier of the button clicked. The pressed button is determined by comparing its value with the tag value (ie. When you submit_edit_category__1, the value "Edit" sent).

0
source share

All Articles