0

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);

}

2 Answers 2

3
<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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! So I should retrieve them without the "[]"? Should I use $_REQUEST instead of $_POST or what do you mean?
@holyredbeard $_POST if you POST, $_GET if you GET, $_REQUEST if I don't know :) [] turns them into arrays in PHP. So you treat $_WHATEVER['newListObject'] as an array not a scalar. And without []. That's just in HTML to make PHP give you arrays.
1

If you call them name="newListPObject[]" PHP will receive them as an array that can be looped over.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.