8

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

4
  • 1
    In a html form I have a variable $var = "some value";. -- how? Can you show us the code? Commented Aug 25, 2013 at 17:23
  • You need to elaborate if it's in the same file, if the form post severes the variable assignment and output, and the sample code not within functions. Commented Aug 25, 2013 at 17:24
  • 3
    php and html have nothing tobdo with each other. you generate html using php and you post back data to php using forms. Commented Aug 25, 2013 at 17:24
  • I am printing the html form if the php variable is true. The same Value I want at the time of reading.. Commented Aug 25, 2013 at 17:35

2 Answers 2

32

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.

Sign up to request clarification or add additional context in comments.

7 Comments

why you have downvoted ?explain please
+1 as downvotes are lame!
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..
good learning and enjoy web development :)
Shouldn't it be start_session()? session_start() doesn't work for me.
|
0

Try that

First place

global $var;
$var = 'value';

Second place

global $var;
if (isset($_POST['save_exit']))
{
    echo $var; 
}

Or if you want to be more explicit you can use the globals array:

$GLOBALS['var'] = 'test';

// after that
echo $GLOBALS['var'];

And here is third options which has nothing to do with PHP global that is due to the lack of clarity and information in the question. So if you have form in HTML and you want to pass "variable"/value to another PHP script you have to do the following:

HTML form

<form action="script.php" method="post">
    <input type="text" value="<?php echo $var?>" name="var" />
    <input type="submit" value="Send" />
</form>

PHP script ("script.php")

<?php

$var = $_POST['var'];
echo $var;

?>

6 Comments

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.
ewww global? he just needs a hidden input. that's it.
I am printing the html form if the php variable is true. The same Value I want at the time of reading..
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.
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.