Pass a PHP variable value through an HTML form

111,423

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.

Share:
111,423

Related videos on Youtube

user2642907
Author by

user2642907

Updated on July 20, 2020

Comments

  • user2642907
    user2642907 almost 4 years

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

    • Amal Murali
      Amal Murali over 10 years
      In a html form I have a variable $var = "some value";. -- how? Can you show us the code?
    • Sergiu Paraschiv
      Sergiu Paraschiv over 10 years
      php and html have nothing tobdo with each other. you generate html using php and you post back data to php using forms.
  • user2642907
    user2642907 over 10 years
    I use the first example but its not working in my case.... O/P of the function I am storing in $var in a form. After its posted I want to print this while reading... but its not working.
  • itachi
    itachi over 10 years
    ewww global? he just needs a hidden input. that's it.
  • Charaf JRA
    Charaf JRA over 10 years
    why you have downvoted ?explain please
  • user2642907
    user2642907 over 10 years
    I am printing the html form if the php variable is true. The same Value I want at the time of reading..
  • Alex Rashkov
    Alex Rashkov over 10 years
    I just don't get why people are down voting correct answers! That's really frustrating! @itachi if you know what he needs you can post an answer, though based on the question context it's not very clear what he's trying to do. Further more I've added that to the answer so depending on what he want to accomplish he can choose the right solution.
  • Alex Rashkov
    Alex Rashkov over 10 years
    +1 as downvotes are lame!
  • itachi
    itachi over 10 years
    you got downvoted because of abuse of global. i didn't downvote but just pointed out. Question is pretty clear. Only he made it complicated by mentioning Scope. It has nothing to do with scope. He just wants to pass that variable once the form is submitted.
  • Alex Rashkov
    Alex Rashkov over 10 years
    OK I totally agree globals are bad but if people want to use them it's their problem, it's part of PHP. As of the questions, at first sight it's a bit misleading especially the title.
  • user2642907
    user2642907 over 10 years
    I got it with the first hidden field... I am sorry I ask wrong question because I dont know what is this called... Now I came to know its called passing a variable through hidden field.. thans..
  • Charaf JRA
    Charaf JRA over 10 years
    good learning and enjoy web development :)
  • saleem ahmed
    saleem ahmed over 8 years
    well explained answer, What is the need of downvote here? upvoted
  • Pukki
    Pukki over 7 years
    Shouldn't it be start_session()? session_start() doesn't work for me.
  • LMD
    LMD over 5 years
    I like the hidden field :)

Related