Pass A Php Variable Value Through An Html Form
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($_PO
Solution 1:
EDIT: After your comments, I understand that you want to pass variable through your form.
You can do this using hidden field:
<inputtype='hidden'name='var'value='<?phpecho"$var";?>'/>
In PHP action File:
<?phpif(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.
Solution 2:
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 thatecho$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
<formaction="script.php"method="post"><inputtype="text"value="<?phpecho$var?>"name="var" /><inputtype="submit"value="Send" /></form>
PHP script ("script.php")
<?php$var = $_POST['var'];
echo$var;
?>
Post a Comment for "Pass A Php Variable Value Through An Html Form"