从链接的textareas的$ _POST数据中获取id

Trying to link two fields on a POST by getting the ID.

Each 'songfld' has a unique number as below. The textarea is linked on that number.

The form fields:

<input type="text" name="songfld[901]" value="">
<textarea name="data[901][name]" id="txt-901" rows="2" cols="80"></textarea>

<input type="text" name="songfld[902]" value="">
<textarea name="data[902][name]" id="txt-902" rows="2" cols="80"></textarea>

And trying to get the 901 & 902 ID's here:

foreach($_POST['songfld'] as $val){
    $val is the value from input
    $id = $_POST['songfld'][0];     // the number in each name="songfld[X]"
    $namefld = $_POST['data'][$id]['name']; // gives the value from textarea

    echo "<P>$id $idfld $namefld";
}

Of course the value passes, but I'd like to have that unique number from each post as well.

hope someone knows this trick.

Inspect your data like this:

echo "<pre>";
print_r($_POST);
echo "</pre>";
exit;

Also try:

echo "<pre>";
print_r($_POST['songfld']);
echo "</pre>";
exit;

You'll see what is exactly in the arrays.

foreach($_POST['songfld'] as $name => $val){
  ...
}

Try this:

foreach($_POST['songfld'] as $key => $val){

   // $key holds the value you are looking for
}
$sInputFields = '';
for( $i=901;$i<903;$i++ ){

    $sInputFields .= '<input type="text" name="songfld[' . $i . ']" value="">';

    $sInputFields .= '<textarea name="data[' . $i . '][name]" id="txt-' . $i . '" rows="2" cols="80"></textarea><br>';
}
echo $sInputFields;

You can try this