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>