htmlformsphp-5.4

Make a PHP variable the value of a form Input


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>
';


Solution

  • 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