简体   繁体   中英

Pass variables from one file to another in PHP

I have a site in which there are many users, and there's a section for editing them. Let's call this section page 'A.php'. When clicking on this page 'A', you get to see a list of all the users registered in the site in form of a table. Then, there's an input space for entering the username you want to edit, this is an HTML form method (POST), that verifies the information and if the username doesn't exist, you get an error page.

But if it does, a new page is opened, let's call this one page 'B.php' and you get to see its full info, as well as more input spaces in case you want to change some of the user values. There's an option for saving changes (page 'C.php') and an option for deleting the user (page 'D.php).

The problem I have right now is that, since in page 'A.php' I send the info via a form method="POST" and it is later received in page 'B.php', I don't know how to re send it to pages 'C.php' and 'D.php', since these pages don't receive any data from a form method="POST". I tried using the include function but it doesn't work, can anybody help me please?

Here's how I'm sending the info from page 'A' to 'B':

Page A:

<form method="POST" action="edit_usr.php">
    <font size = "5">Username: <input type="text" name="username" />
    <input type="submit" name="submit" value="EDIT USER" /></font>
</form>

This is how page B (edit_usr.php) receives the code:

$usr_mod = $_POST['username'];

I assume the only thing I need to do is to export the variable $usr_mod to pages 'C' and 'D', but I've already looked for it on this page and the PHP manual and I could't find anything. Can anybody please help me? It would be really appreciated guys! :)

store the value in hidden field and call value is next page.In Page B <input type="hidden" name="username" value=<?php echo $usr_mod = $_POST['username']; ?>" > <input type="hidden" name="username" value=<?php echo $usr_mod = $_POST['username']; ?>" > assign the hidden field and call the value in C.

Why don't you pass them in the GET parameter something like:

<a href="saveChanges.php?usr_mod =<?php echo $usr_mod; ?>"> Click Here to Save Changes </a>

and in saveChanges.php

$usr_mod = $_GET['usr_mod'];

As the comment mentioned, sessions are probably the way to go here.

  1. At the top of edit_usr.php , write session_start(); .
  2. At the top of page C and page D, also write session_start(); .
  3. Inside edit_usr.php , write $_SESSION['usr_mod'] = $usr_mod; . Alternately, you can just write $_SESSION['usr_mod'] = $_POST['username']; and just use $_SESSION['usr_mod'] in place of $usr_mod but that's a matter of preference.
  4. Inside page C and page D, just use $_SESSION['usr_mod'] to access the variable.

When the user logs out, make sure you destroy the session.

session_unset();
session_destroy();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM