Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a PHP variable value through an HTML form [duplicate]

Tags:

html

forms

php

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 ??

like image 331
user2642907 Avatar asked Aug 25 '13 17:08

user2642907


1 Answers

EDIT: After your comments, I understand that you want to pass variable through your form.

You can do this using hidden field:

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

In PHP action File:

<?php 
   if(isset($_POST['var'])) $var=$_POST['var'];
?>

Or using sessions: In your first page:

 $_SESSION['var']=$var;

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.

like image 64
Charaf JRA Avatar answered Sep 28 '22 19:09

Charaf JRA