tags:

views:

92

answers:

2

Hello! I have a for loop which actually displays a product name and several buttons like: Edit, Update , Cancel For each product i am displaying , it will have its own set of Edir, Update, and Cancel button as below.

Paint Edit Update Cancel

I want to loop through the buttons so that for each category, I can perform a different action. I was thinking about using something like btn_edit1, btn_edit2 for the name of the button and use a for loop. 1, 2 are the category ids. Maybe Im not clear enough. Sorry for that. Can anyone give me some suggestions?

for($i = 0; $i<count($obj_categories_admin->categories);$i++)
{   

            echo "<tr>";

            echo "<td width='1500'>";
            echo "<input type='text' name='name'  size = '30' value='" . $obj_categories_admin->categories[$i]['name'] . "'/>";

            echo "</td>";

            echo "<td width='500'>";

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

            echo "</td>";



            echo "<td width='500'>";

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

            echo "</td>";

            echo "<td width='500'>";
            echo "<input type='submit' value = 'Delete' name='submit_delete_category_" . 
            $obj_categories_admin->categories[$i]['category_id'] . "'/>";
            echo "</td>";

            echo "<td width='500'>";

            echo "<input type='submit' value = 'Cancel' name='cancel'" . "'/>" ;

            echo "</td>";

            echo "</tr>";   
    }

I want to do something like

foreach($_POST as $key => $value)
{

}

so that when i click on a button it performs an action depending on the category_id.

I have tried this as suggested:

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

Now in my class, i have:

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

But, i am getting the error : undefined index submit

+1  A: 

Well i would use something like this:

<fieldset>
<!-- product info -->
<input name="productName[paint]" />
<input name="productName[edit]" />
<input name="productName[delete]" />
<input name="productName[cancel]" />
</fieldset>

that way when you get it to the serverside everything will be nice and tidy in nested arrays.

prodigitalson
+1  A: 

I would give the name attributes of my submit buttons using following pattern:

name="submit[which_action][which_category]"

For example for your 'Update' button for category 123:

name="submit[update][123]"

When the user clicks any of the submit buttons, to determine which specific button the user has clicked you just need check for $_POST['submit'] in your PHP code:

$a1 = $_POST['submit'];
$which_action = reset(array_keys($a1));
$which_category = reset(array_keys($a1[$which_action]));
Lukman
Thanks! Im going to try it! :)
chupinette