I'm trying to get the index of an HTML element and pass this to a PHP variable to eventually upload to a database.
<form method="post" action="insert.php" enctype="multipart/form-data">
....
<div><textarea name="paragraph[]"></textarea></div> //index 0
<div><textarea name="paragraph[]"></textarea></div> //index 1
<div><textarea name="paragraph[]"></textarea></div> //index 2
....
</form>
if(isset($_POST['paragraph'])) {
foreach ( $_POST['paragraph'] as $paragraph){
//get index of container div for this paragraph and store it in a variable
}
}
Looking at this Stack Overflow answer, I'm guessing I should do something like:
$divs = $dom->getElementsByTagName('div');
However, instead of getting all the <div>
elements and storing them in an array, is there any way I can get the current <div>
element from within the foreach
loop and store its index in a variable?
Some clarification: The form is dynamic. So it could look like this, too:
<form>
<div><textarea name="paragraph[]"></textarea></div> //index 0
<div><textarea name="something_else"></textarea></div> index 1
<div><textarea name="paragraph[]"></textarea></div> //index 2
<div><textarea name="paragraph[]"></textarea></div> //index 3
</form>
You could use a base array to hold all your items such as the below HTML:
<div><textarea name="items[][paragraph]"></textarea></div> //index 0
<div><textarea name="items[][something_else]"></textarea></div> index 1
<div><textarea name="items[][paragraph]"></textarea></div> //index 2
<div><textarea name="items[][paragraph]"></textarea></div> //index 3
Then when you access $_POST['items']
you will have them listed with their indexes:
array (size=1)
'items' =>
array (size=4)
0 =>
array (size=1)
'paragraph' => string 'test' (length=4)
1 =>
array (size=1)
'something_else' => string 'test 2' (length=6)
2 =>
array (size=1)
'paragraph' => string 'test 3' (length=6)
3 =>
array (size=1)
'paragraph' => string 'test 4' (length=6)
You can get all the information held in this array easily using the below loop:
foreach($_POST['items'] as $index => $item) {
echo "Index is: " . $index;
echo "Key is: " . key($item);
echo "Value is: " . $item[key($item)];
}
Which will print:
Index is: 0
Key is: paragraph
Value is: test
Index is: 1
Key is: something_else
Value is: test 2
etc.
If you want to get all values for paragraph
in one easy swoop you can use array_column to access these, for example the below:
array_column($_POST['items'], 'paragraph');
Will print:
array (size=3)
0 => string 'test' (length=4)
1 => string 'test 3' (length=6)
2 => string 'test 4' (length=6)
The paramaeters sent to PHP are just the inputs, there's no information about the HTML that contains the inputs. So you can't get the DIV indexes, because they're not part of the inputs.
You could change your HTML so that the input names include the DIV indexes.
<form>
<div><textarea name="paragraph[0]"></textarea></div> //index 0
<div><textarea name="something_else"></textarea></div> index 1
<div><textarea name="paragraph[2]"></textarea></div> //index 2
<div><textarea name="paragraph[3]"></textarea></div> //index 3
</form>
Since you're generating the HTML dynamically, the loop that creates it should be able to insert the DIV index into the names of the text areas.
Then your PHP can get the indexes by adding to the foreach
.
foreach ($_POST['paragraph'] AS $divindex => $paragraph)