0

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

2
  • Do you post the font_syle param in page.php?? Commented Sep 28, 2012 at 19:49
  • so you want to print the $font_style in second page Commented Sep 28, 2012 at 19:49

3 Answers 3

4

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.

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

2 Comments

The easiest way would be to just reference the $_SESSION variable directly
Add the end of action_form.php, add a line $_SESSION['font_size'] = $font_style;. Then you can print it on other pages with echo $_SESSION['font_size'];
0

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

Comments

0

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.

4 Comments

I want to use the result of the form in multiple pages.
@user1689166 Then store this either inside a database or inside the array $_SESSION and on every page call session_start() on the first line.
How can I make "$total" to be inside the $_SESSION?
@user1689166: Google can help here sometimes. $_SESSION is an array so treat it as such, e.g. $_SESSION['foo'] = "bar". php.net/manual/en/language.types.array.php

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.