Forum Moderators: coopster

Message Too Old, No Replies

passing variables with forms

         

swati

3:39 pm on Oct 14, 2004 (gmt 0)

10+ Year Member



Hello,

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

JamesRock

3:46 pm on Oct 14, 2004 (gmt 0)

10+ Year Member



Hi 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.

swati

4:02 pm on Oct 14, 2004 (gmt 0)

10+ Year Member



Hello JamesRock,
thank you so much for your prompt reply...I m gonna try it out right now n get back to u with what happened....
thanks a million
Regards
Swati

swati

4:31 pm on Oct 14, 2004 (gmt 0)

10+ Year Member



Hi
wat u said works perfectly(both options).
Now however my code is more complicated.

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

JamesRock

12:54 am on Oct 15, 2004 (gmt 0)

10+ Year Member



Well again if you add a hidden field to your form in page2.php like so:


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

swati

10:18 am on Oct 15, 2004 (gmt 0)

10+ Year Member



it works!
thank you so much ..

Regards
Swati