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

Recommended Answers

All 6 Replies

use "" i.e

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

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.

@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;

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

Member Avatar for LastMitch

@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'];

?> 

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.