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.
Related videos on Youtube
Author by
user2642907
Updated on July 20, 2020Comments
-
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 over 10 years
In a html form I have a variable $var = "some value";.
-- how? Can you show us the code? -
Sergiu Paraschiv over 10 yearsphp and html have nothing tobdo with each other. you generate html using php and you post back data to php using forms.
-
-
user2642907 over 10 yearsI 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 over 10 yearsewww
global
? he just needs a hidden input. that's it. -
Charaf JRA over 10 yearswhy you have downvoted ?explain please
-
user2642907 over 10 yearsI am printing the html form if the php variable is true. The same Value I want at the time of reading..
-
Alex Rashkov over 10 yearsI 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 over 10 years+1 as downvotes are lame!
-
itachi over 10 yearsyou 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 mentioningScope
. It has nothing to do with scope. He just wants to pass that variable once the form is submitted. -
Alex Rashkov over 10 yearsOK 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 over 10 yearsI 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 over 10 yearsgood learning and enjoy web development :)
-
saleem ahmed over 8 yearswell explained answer, What is the need of downvote here? upvoted
-
Pukki over 7 yearsShouldn't it be
start_session()
?session_start()
doesn't work for me. -
LMD over 5 yearsI like the hidden field :)