Question

I want to pass a variable to another page by form data. I've looked at other tutorials and codes, i've followed them closely, but the contents of the variable doesn't output. Basically php doesn't work inside the value="". Here's the code.

page 1.

 <?php
    $hi = 1224;
 ?>
    <form method = "post" action = "page2.php">
    <input type = "hidden" Name = "var" Value = "<?phpecho$hi;?>"/>
    <input type = "submit" Name = "enter" Value = "Submit"/>
    </form>

page 2.

 <?php
    $test = $_REQUEST['var'];
    echo $test;
 ?>

Nothing is outputted. I've even tried session variables but somehow they worked but once i refreshed the page, the variables were reset. I've started sessions on all pages etc..

Was it helpful?

Solution 2

When something isn't working, make sure to check the source code for error, don't just look browser's rendering. In the source, you'd clearly have seen that <?phpecho$hi;?> wasn't evaluated, but instead just printed in the source.

So in conclusion, <?phpecho$hi;?> is wrong. Write <?php echo $hi;?>.

OTHER TIPS

It should be <?php echo $hi; ?> instead of <?phpecho$hi;?>. i.e. you need give space between <?php and echo and its $hi variable as like in below.

 <input type = "hidden" name = "var" value = "<?php echo $hi; ?>"/>

Change your line

 <input type = "hidden" Name = "var" Value = "<?phpecho$hi;?>"/>

to

 <input type = "hidden" name = "var" value = "<?php echo $hi; ?>" />

If possible change the name of the hidden textbox to something relevant instead of var , because var is a reserved word in javascript to avoid confusion.

Instead of

<input type = "hidden" Name = "var" Value = "<?phpecho$hi;?>"/>

Should be, you need spaces in php

<input type = "hidden" name = "var" value = "<?php echo $hi; ?>"/>

try this look to $_POST super global for value :

 <?php
    $test = $_POST['var'];
    echo $test;
 ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top