Reputation: 21198
I have a form that you dynamically creates new input fields (type=hidden) which when the form is sent should be retrieved by my php code. However, by the reason that the number of input fields can differ I gave them the same name. The problem however is that I don't know how to retrieve it, or more correctly, what to do with what's retrieved.
from the form:
...
<input type='hidden' name='newListObject' value='0' />
<input type='hidden' name='newListObject' value='1' />
<input type='hidden' name='newListObject' value='2' />
<input type='hidden' name='newListObject' value='3' />
...
from php code (listView.php):
private $m_newListObject = 'newListObject';
...
if (isset($_POST[$this->newListObject])) {
$listObjects = $_POST[$this->m_newListObject];
}
from php code (listModel.php):
//Below doesn't work because $listObjects isn't an array
foreach ($listObjects as $listObject) {
$query = "INSERT INTO listElement (listElemName, listId) VALUES(?, ?)";
$stmt = $this->m_db->Prepare($query);
$stmt->bind_param('si', $listObject, $listId);
$ret = $this->m_db->RunInsertQuery($stmt);
}
Upvotes: 0
Views: 189
Reputation: 3065
If you call them name="newListPObject[]"
PHP will receive them as an array that can be looped over.
Upvotes: 1
Reputation: 12985
<input type='hidden' name='newListObject[]' value='0' />
<input type='hidden' name='newListObject[]' value='1' />
<input type='hidden' name='newListObject[]' value='2' />
<input type='hidden' name='newListObject[]' value='3' />
And use $_REQUEST['newListObject']
as an array()
now.
Upvotes: 3