简体   繁体   中英

How to combine a PHP variable and $_REQUEST?

I am retrieving data from a dynamic form. For that I have counted the number of elements on the form and made each input have a unique name, using the counter variable, eg:

echo "<input type='hidden' name='itemid" . $i . "' value='" . $row['itemid'] . "' />";

On the relieving page I need to modify $_REQUEST[] to include its own counter variable, eg:

while($x <= $counter){

  $itemid = trim($_REQUEST['itemid" . $x . "']);

  $x = $x + 1;

};

Does anyone know the correct way of combining the $_REQUEST and $x variable?

$itemid = trim($_REQUEST['itemid" . $x . "']);

should be

$itemid = trim($_REQUEST['itemid' . $x]);

A better way to do this would be to use arrays. Then you don't need to use a counter to keep track of where you are:

echo "<input type='hidden' name='itemid[]' value='" . $row['itemid'] . "' />";

PHP:

foreach ($_REQUEST['itemid'] as $item) {
    // do stuff
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM