user1163722
user1163722

Reputation:

Getting $_POST variables that contain variables

<?php

$var = "arbitrary";

echo <<<_END

<form method = "post" action = "feed.php">

    <input type = "text" name = "$var" />
    <input type = "submit" />

</form>

_END;

if (isset($_POST['$var']))
{
    echo $_POST['$var'];
}

?>

I can't seem to get this $_POST data when I try to retrieve it from the variable $var. Is this possible? (Getting $_POST from something other than just a string)

Upvotes: 0

Views: 6446

Answers (2)

Gohn67
Gohn67

Reputation: 10648

Try this:

<?php

$var = "arbitrary";

?>

<form method = "post" action = "feed.php">

    <input type = "text" name = "<?php echo $var ?>" />
    <input type = "submit" />

</form>

<?php

if (isset($_POST[$var]))
{
    echo $_POST[$var];
}

?>

The issue was just the quoted $var in your array syntax here $_POST['$var'] as others have mentioned. There was nothing wrong with your heredoc syntax, so if you feel more comfortable with HERDOC syntax, you should continue with it.

Upvotes: 2

David Nguyen
David Nguyen

Reputation: 8508

$_POST['$var'] should be $_POST[$var] aka don't wrap your variable in a single quote

Upvotes: 6

Related Questions