user2642907 user2642907 - 1 year ago 98
PHP Question

Pass a PHP variable value through an HTML form

In a html form I have a variable

$var = "some value";

I want to call this variable after the form posted. The form is posted on the same page.

I want to call here

if (isset($_POST['save_exit']))

echo $var;


But the variable is not printing. Where I have to use the code GLOBAL ??

Answer Source

EDIT: After your comments,i understand that you want to pass variable throw your form :

You can do this using hidden field:

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

In PHP action File:

   if(isset($_POST['var'])) $var=$_POST['var'];

Or using sessions: In your first page:


start_session(); should be placed at the beginning of your php page.

In PHP action File:

if(isset($_SESSION['var'])) $var=$_SESSION['var'];

First Answer:

You can also use $GLOBALS :

if (isset($_POST['save_exit']))

   echo $GLOBALS['var']; 


Check this documentation for more informations.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download