Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to alter two different tables from an HTML post request to a server running PHP. Before, when I only wanted to alter one table, all the columns and values were put into the POST array, so I would have [colname1]=>val1, [colname2]=val2, etc. I would then loop through the POST to build the correct MySQL statement to run the query.

I've modified my code so that depending on what drop down box is selected, not only will the first table get modified, but another table (of the users choice) will get modified as well. So ideally I would like the POST variable in PHP to have an array of arrays. So in the end, it would be nice if the POST variable looked like this...

[mainTable][colnam1]=>val1 [colnam2]=>val2
[otherTable][colnam1]=>val1 [colnam2]=>val2

My current workaround is having to give the table name in front of the the form input name and then parsing it on the server, but I would rather be more flexible and not do it this way.

<input type="text" name="mainTable_colname">
<input type="text" name="otherTable_colname">

From my understanding of using forms and POST, only the named html form elements will be passed up into the post variable.

Does anyone know a better way of doing this?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

Specify the name attribute of your elements to be arrays, including the keys you want, and you'll get arrays in $_POST:

<input type="text" name="mainTable[colname]">
<input type="text" name="mainTable[colname2]">
<input type="text" name="otherTable[colname]">

A var_dump( $_POST['mainTable']); would yield something similar to:

array( 'colname' => 1, 'colname2' => 1);
share|improve this answer
    
Wow thanks! How easy was that! I wasn't expecting such an easy solution! Much appreciated! –  Matthew Jul 5 '12 at 20:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.