Forum Moderators: coopster
I have page1 and page2. I have a post method form in page1 through which I want to pass a variable "$name" to page2.php
I tried the following
<form method="post" action="page2.php?var=<?php echo $name;?>">
And in page2.php accessed it as $_POST['var']. But it contained nothing. I also tried just $var but no luck.
I want to access this variable $name in the next page and that too only thru a form.
Please give me the ways i can do it or tell me where I am going wrong.
Help!
Regards
Swati
Your problem is that you are trying to access it as a POST variable but it is actually a GET variable.
Try using $_GET['var'] in page2.php
You see, the form is a post form but you are passing the variable in the query string (the action of the form) so PHP sees it as a GET variable.
If you want it to come through as a post variable, this is what you need to do:
<form method="post" action="page2.php">
<input type="hidden" name="var" value="<?=$name?>" />
...
This uses a hidden form element to pass the value through, you can then access it in page2.php as
$_POST['var'] Note I used the shorthand version of:
<?php echo $name;?> as
<?=$name?> Learn this, it makes your code look neater.
Hope this helps.
I have a form with an "add" button inside the form. The form action tag is set to itself so upon clicking the button the page will be re-loaded.
I want to make use of the value of the variable when the "add" button is clicked.
The problem is that when I clik on the button the value in the $name disappears. It contains nothing.
I want a way such that I can still access the variable inside this form (or after any event that re-loads this page)
(the first time i come to page2, the value is intact however when it reloads the value is gone)
The code:
<form method="post"
action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="submit" name="add" value="Add Associated Test Equipment">
<?php echo("name '".$_POST['name']."'");
// here the value is available
if($_POST['add']){
echo("name is '".$_POST['name']."'");
//here value available only for once but not after a reload
$sql1 = @mysql_query("select idTest_Equipment from Test_Equipment where name = '".$_POST['name']."'");
..
..
..some more things
</form>
So you see from the name i want to obtain another detail. If i dont get the name i cant work further....
I need the name when the user clicks on the "Add Associated Test Equipment" button
Help...
Regards
Swati
<form method="post"
action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="submit" name="add" value="Add Associated Test Equipment"> [b]<input type="hidden" name="name" value="<?=$_POST['name']?>" />[/b]
<?php echo("name '".$_POST['name']."'");
// here the value is available
if($_POST['add']){
echo("name is '".$_POST['name']."'");
//here value available only for once but not after a reload
$sql1 = @mysql_query("select idTest_Equipment from Test_Equipment where name = '".$_POST['name']."'");
..
..
..some more things
</form>
Then the variable will be available on the next page load.
Since you are submitting a form, the value will not be passed along unless you make that field part of that form through a hidden tag.