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

Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58
user2642907
  • 121
  • 1
  • 1
  • 11
  • 1
    `In a html form I have a variable $var = "some value";.` -- how? Can you show us the code? – Amal Murali Aug 25 '13 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. – mario Aug 25 '13 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. – Sergiu Paraschiv Aug 25 '13 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.. – user2642907 Aug 25 '13 at 17:35

2 Answers2

31

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.

Louis
  • 167
  • 1
  • 13
Charaf JRA
  • 8,249
  • 1
  • 34
  • 44
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;

?>
Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58
  • 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. – user2642907 Aug 25 '13 at 17:33
  • ewww `global`? he just needs a hidden input. that's it. – itachi Aug 25 '13 at 17:33
  • I am printing the html form if the php variable is true. The same Value I want at the time of reading.. – user2642907 Aug 25 '13 at 17:37
  • 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 Aug 25 '13 at 17:38
  • 2
    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. – itachi Aug 25 '13 at 17:41
  • 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. – Alex Rashkov Aug 25 '13 at 17:47