0

I want to make a PHP variable the value of a hidden form input. The form is inside of my PHP (I'm echoing the form), and nothing that I have tried works.

Here's my code:

echo '
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>

<!-- Here is where I need to make my PHP variable the value: -->
<input type = "text" name = "referer" style = "display: none" value = "$variable"> 

<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
';

trincot
  • 317,000
  • 35
  • 244
  • 286
user5824608
  • 45
  • 2
  • 7
  • `value = "' . $variable . '"> ` the text inside `' '` is treated as literal and php won't parse the variables. – bansi Jan 23 '16 at 03:37

3 Answers3

0

The usual string replacements "$var" don't work here , as it's all contained within a single quote string which doesn't allow for string replacements. You will have to concatenate manually

echo '
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>

<input type = "text" name = "referer" style = "display: none" value = "' . $variable . '"> 

<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
';
Joseph Young
  • 2,758
  • 12
  • 23
  • 1
    in php `.` is used for [concatenation](http://php.net/manual/en/language.operators.string.php) not `+` – bansi Jan 23 '16 at 03:42
0

Try this:

?><!-- exit out of php into html-->
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>

<!--Here is where I need to make my PHP variable the value:-->
<input type = "text" name = "referer" style = "display: none" value = "<?=$variable?>">

<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
<?php // enter back into php

The <?= ?> is a php short tag


Also, if you still want to use echo, try this:

//note: I changed the quotes
echo "
<div id = 'login'>
<form action = 'process.php' method = 'POST'>
Name: <input type = 'text' name = 'name' required>

<input type = 'text' name = 'referer' style = 'display: none' value = '$variable'> 

<input type = 'submit' name = 'submit' value = 'Enter'>
</form>
</div>
";

See this Q/A for more info

Community
  • 1
  • 1
Isaac
  • 11,409
  • 5
  • 33
  • 45
0

Try this sample here https://eval.in/506642

   echo "
<div id = 'login'>
<form action = 'process.php' method = 'POST'>
Name: <input type = 'text' name = 'name' required>

//Here's where I need to make my PHP variable the value:
<input type = 'text' name = 'referer' style = 'display: none' value = '$variable'> 

<input type = 'submit' name = 'submit' value = 'Enter'>
</form>
</div>
";
Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67