0

I need to pass a variable from page1 to page2 without URL, or without making the variable visible to users, to I figured, I might use the hidden field method, but I can't do this with the following script

<form method="POST" action="page1.php">

                Username: <input type="text" maxlength="25" name="username" value="" class="i_login" />  
                Password: <input type="password" maxlength="32" name="password" value=""   class="i_login" /> 
                          <input type="hidden" name="hidden_field" value=""   class="i_login" /> 
                <input type="submit" name="login" value="Login" class="submit"/>   

And, in page2.php I made

$hidden = $_POST['hidden_field'];
echo $hidden;

But nothing happens

5
Contributors
6
Replies
9
Views
5 Years
Discussion Span
Last Post by moneeshot
1

If the example provided in your post is what you are using, then you are not getting any output because of the following errors:
- Your form action is page1.php, not page2.php, so you never POST the data to the second script.
- The value attribute of your hidden tag is empty, so if you echo it you won't see anything.

Also, for the purposes of this script, there would be no reason for using get over post.

2

@HunainHafeez
Your suggestion of using double quotes over single quotes will not have any effect on the result.
The difference between double and single quotes is whether or not PHP will try to parse variable within the string or not. A single quoted string will be treated as a literal string, wheras a double quoted one will result in any variables being parsed to their values.

$var1 = 'This is a variable';

$string = 'Variable: $var1';
// Will output Variable: $var1
echo $string;

$string2 = "Variable: $var1";
// Will output Variable: This is a variable
echo $string2;

Edited by Will Gresham

0

Actually, I made a mistake when I posted the code, because, I am posting from page from page2 to page1. Username and password, get echoed finely, but not the hidden filed. The files are fine, it is just the hidden field that is not working

0

@Vribium

it is just the hidden field that is not working

What is the valve in the hidden input?

<input type="hidden" name="hidden_field" value="" class="i_login" />

I presume you have a data from a previous page. Then you can used

$_session

On page2.php

You do this:

<? 

session_start();

if (isset($_POST['submit'])) { 

$_session['hidden_field'] = $_POST['hidden_field'];

} 

 echo $_session['hidden_field'];

?> 
0

Did you give the field a value? printing an empty string will give you no results. You would have to use something like var_dump() to at least recieve some type of acknowledgement that the variable was past in the post. I see no reason to use a session for this, not at this point?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.