I have a page with two forms in it. The first has an action of "#" and the second of "test3.php". The first works fine to perform its functions and the second does load the desired file but the second is not passing any variables. I'm not sure what information you need. The coding is quite long. Does anyone have any ideas?
2 Answers
You can use sessions for this.
As an example, I've set up 3 different bodies of code. This method is based on entering a username and password.
It doesn't matter what you enter, it won't do anything but post the variables on all subsequent pages.
This is to give you a general feel of how sessions work and how variables can be used throughout different pages.
Page 1
<?php
ob_start();
session_start();
if(isset($_POST['submit'])){
$_SESSION['myusername'] = $_POST['myusername'];
$_SESSION['mypassword'] = $_POST['mypassword'];
echo $_SESSION['myusername'];
echo "<br>";
echo $_SESSION['mypassword'];
} // isset submit
ob_end_flush();
?>
<form action="" method="post">
Username:
<input type="text" name="myusername">
<br>
Password:
<input type="text" name="mypassword">
<br>
<input type="submit" name="submit" value="Submit">
</form>
<br><br>
<a href="check_page2.php">check page 2</a>
Page 2 (check_page2.php)
<?php
session_start();
if (isset( $_SESSION['myusername']) && isset( $_SESSION['mypassword']) ){
$myusername = $_SESSION['myusername'];
$mypassword = $_SESSION['mypassword'];
echo $myusername;
echo "<br>";
echo $mypassword;
}
?>
<br><br>
<a href="check_page3.php">check again on page 3</a>
Page 3 (check_page3.php)
<?php
session_start();
if (isset( $_SESSION['myusername']) && isset( $_SESSION['mypassword']) ){
$myusername = $_SESSION['myusername'];
$mypassword = $_SESSION['mypassword'];
echo $myusername;
echo "<br>";
echo $mypassword;
}
?>
You could even use a session variable with jQuery.
Just add: (to check_page3.php) after the closing ?> PHP tag:
<a href="check_jquery.php">check jQuery variable in source</a>
And inside that, you can use: (check_jquery.php)
which will show the username variable inside the HTML source. (as an example)
<?php
session_start();
if (isset( $_SESSION['myusername']) && isset( $_SESSION['mypassword']) ){
$myusername = $_SESSION['myusername'];
$mypassword = $_SESSION['mypassword'];
echo $myusername;
echo "<br>";
echo $mypassword;
}
?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
var usersession = "<?php echo $_SESSION['myusername']; ?>";
// alert (usersession);
});
</script>
Footnotes:
Generally using type="password" would need to be used for:
<input type="text" name="mypassword"> but this was just an example.
The actual syntax would be:
<input type="password" name="mypassword"> in order to mask an entered password.
sessionswhich can be passed from almost anywhere/pages.