0

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
  • 1
    Either save the first data to $_SESSION or $_COOKIE, or add the old inputs of first form as hidden form elements to the second form. You can even store them in the database. Commented Feb 21, 2014 at 15:01
  • 2
    You could setup a pastebin file, if the code is too long, however if at all possible, post the relevant code in question. You could use sessions which can be passed from almost anywhere/pages. Commented Feb 21, 2014 at 15:01

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I figured it out finely. I have combined both files into one. I am using POST and GET within the same file to move variables between forms and then updating the database that was on the second page right within the first page. Thank you everyone for your input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.