简体   繁体   中英

PHP Session variables in multiple pages

So, here I have page1.php:

<form action="action_form.php" method="post">
<select name="font_syle">
<option value="tahoma">Tahoma</option>
<option value="arial">Arial</option>
</select>
<input type="submit" value="Done" />
</form>

Here action_form.php:

<?php
session_start();
$font_style = $_POST["font_syle"];
$_SESSION["font_syle"] = $font_style;
if($_SESSION["font_syle"] == 'tahoma') $font_style = 10;
else if($_SESSION["font_syle"] == 'arial') $font_style = 20;

$total = $font_style;

echo $total;
?>

And here page.php

<?php 
ob_start();
include 'action_form.php';
ob_end_clean();

echo $total;
?>

I don't know why the value of "$total" is not printed on page.php

page.php includes action_form.php . That sets the value of $font_style to:

$font_style = $_POST["font_syle"];

Since page.php hasn't just been posted through a form, it's setting $font_style to an empty string. So when you come to echo it out, there's nothing there to echo.

You can do echo $_SESSION["font_syle"]; in the page.php to print it

The reason is your form is going to action_form.php and store the data inside the variable $_SESSION .

When you open page.php the data doesn't exist anymore because $total does not move between page.

The solution here is to change :

<form action="action_form.php" method="post">

for

<form action="page.php" method="post">

OR

Print out the session variable instead.

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