Search code examples
phpsession-variablessession-state

Echo user input from one .php file to echo on another


Im having trouble getting the user input entered from one page (index.php) to be echo'd on the next page (receipt.php) but for some reason it keeps saying "undefined index: cleanEmail ...". Im using "session start" for all my pages.

So far iv got a variable made in tools.php called "cleanEmail" that will post the data entered into the specified field from the index.php page(email field in this case) and i tried calling it on the receipt.php page using echo and $_SESSION['cleanEmail'].

Iv tried include(index.php); in the receipt.php page but for some reason after doing that the index and receipt page will be stuck forever in loading and never actually load the page, iv tried include(tools.php); in the receipt page and pretty much crashes the index page for some reason.

Relevant code from tools.php:

<?php
session_start();
include("bookings.txt");
?>

$cleanEmail = $_POST['cust']['email'];

Relevant code from index.php:

<?php
require("receipt.php");
?>
<?php
session_start();
$title = "Lunardo Cinema";
$style="style.css";
include("tools.php")
?>

<form method="post" action="https://generic_website_name/receipt.php">

<label for="email">Email</label>
<span class ="error">* <?php echo $emailErr;?></span>
   <input type="text" id="cust-email" name="cust[email]" placeholder="JohnMoreDoe@gmail.com" >
</form>

Relevant code from receipt.php:

<?php
session_start();
$title = "Receipt Page";

?>

<?php

echo("{$_SESSION['cleanEmail']}"."<br />");

?>

what i need the code to do is When an email is entered in by a user in the input field for email, it will save that input in session and then i can later call it on another page using the same session.

If you want to see all 3 .php files with all the code in it i can upload a link to download a .rar file containg the 3 .php files and the .css file (assuming we are allowed to post links here)


Solution

  • You never store $_SESSION['cleanEmail'] in tools.php

    $cleanEmail = $_POST['cust']['email'] will not becomes session variable

    To store session variable, you have to write like this:

    <?php
    
    $cleanEmail = $_POST['cust']['email']
    $_SESSION['cleanEmail'] = $cleanEmail;
    
    ?>