acr
acr

Reputation: 1746

variable value inside $_POST

I have a range of textboxes named from fa-select-1 to fa-select-8 and I need to echo content from each of these textboxes using $_POST['fa-select-1'];

Currently I am using separate code for each id and echoing it like

$fa1 = $_POST['fa-select-1'];
echo = "fa-select-1 value is $fa1";

I am doing the same from fa1 to fa8. I am changing this do using a while loop.

$x=1; 
while($x<=8)
    {
$fa = $_POST['fa-select-1'];
echo "fa-select-$x value is $test".PHP_EOL;
$x++;
} 

but here I need to loop through $_POST['fa-select-1']; to $_POST['fa-select-8']; in $fa = $_POST['fa-select-1'];. How can I do that ? in javascript i can mention it like $('#fa-select-'+x) if x is javascript variable.

Upvotes: 0

Views: 84

Answers (4)

Sergey Krivov
Sergey Krivov

Reputation: 372

for ($i = 1; $i <= 8; $i++) {
    $fa = $_POST['fa-select-' . $i];
    echo 'fa-select-' . $x . 'value is ' . $test . PHP_EOL;
}

Upvotes: 0

Tun Zarni Kyaw
Tun Zarni Kyaw

Reputation: 2119

please try with this ...

<?php
$x=1; 
while($x<=8){
    $fa = $_POST["fa-select-$x"];
    echo "fa-select-$x value is $fa <br />";
    $x++;
}
?>

Upvotes: 1

bfavaretto
bfavaretto

Reputation: 71918

Concatenate:

$x=1; 
while($x<=8)
{
    $fa = $_POST["fa-select-$x"];
    echo "fa-select-$x value is $test".PHP_EOL;
    $x++;
} 

Upvotes: 0

ComFreek
ComFreek

Reputation: 29424

Just use the concatenation operator (which is a '.' in PHP as opposed to JS where it is a '+' (for strings)):

$_POST['fa-select-' . $x];

You can also directly embed $x in the double-quoted string:

$_POST["fa-select-$x"];



Despite that, you should escape the output in the case some evil user sends HTML in the data:

echo htmlspecialchars($_POST['fa-select-' . $x]);

Upvotes: 1

Related Questions