PHP Session variables in multiple pages - php

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.

Related

PHP Errors aren't showing

I have a simple HTML script:
<form action="" method="post">
<input type="text" name="amount">
<?php echo $amountError; ?>
<input type="submit">
</form>
And I display errors using this:
<?
$amount = $_POST['amount'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (empty($_POST['amount'])) {
$amountError = 'Empty amount';
} else {
header("Location: apmoketi.php");
}
?>
In the apmoketi.php file there is only one line
echo $amount = $_POST['amount'];
However, when I'm trying to display data in the apmoketi.php page, I don't get the value of the $amount variable using action="", but using action="apmoketi.php" I can get it but my PHP code doesn't checking for any errors, why?
A call to header("Location: apmoketi.php"); makes the browser follow the specified URL as another, completely separate request. This new request doesn't have access to the POST data of the previous one.
Your options:
use include so apmoketi.php has access to the variables of the current script
use sessions to preserve data between requests
use a query parameter to carry the value over:
header('Location: apmoketi.php?amount=' . urlencode($amount));
When you redirect to header("Location: apmoketi.php");, the variables are no longer in $_POST[] so you need to set a session variable before you redirect:
$_SESSION['amount'] = $amount;
the way i do it is i have a folder called action with subfiles in there lets say the file called file2 in this file all the php action happens.
and in the root of my folder i have another file called file1 and in here theres the html and now how i would do it is
<form action="action/file2.php" method="post">
<input type="text" name="amount">
<?php echo $amountError; ?>
<input type="submit">
</form>

PHP server redirect

I have been trying to use server side redirect for the drop down menu on my main page. I need the selected option to be remembered using cookies so that it will redirect to the last visited option itself.
This is what I have done so far, but couldn't able to figure it out where I m doing mistake.
<?php
if (isset($_POST['submitted'])){
$newcity=$_POST['city'];
#set cookies
setcookie("city",$newcity,time()+22896000);
}
if ((!isset($_COOKIE['city']) )){
$city = "";
}
else{
header("location:http://example.com/".$_COOKIE['city']."");
}
?>
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method ="POST">
<select name="city">
<option value ="toronto">Toronto</option>
<option value ="ottawa">Ottawa</option>
<option value ="kingston">Kingston</option>
</select>
<input type="submit" name="submitted" value="Submit">
</form>
<?php
if (isset($_POST['submitted'])){
$newcity = $_POST['city'];
setcookie("cookie_city", $newcity, time()+22896000);
}
if ((!isset($_COOKIE['cookie_city']))){
$city = "";
} else{
header("Location: http://example.com/".$_COOKIE['cookie_city']);
exit(0); // <<< Try this
}
?>
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method ="POST">
<select name="city">
<option value ="toronto">Toronto</option>
<option value ="ottawa">Ottawa</option>
<option value ="kingston">Kingston</option>
</select>
<input type="submit" name="submitted" value="Submit">
</form>
Two thoughts:
Try use exit(0) after the redirect.
Use a cookie name different from your POST and GET variables. Maybe your server is configured in some way that overrides POST, GET and cookies.
Your issue is present because you can't access a cookie value on the same page that you set it. Hence why it only works when you reload the page.
You'd be best to redirect them right after the cookie setting:
if (isset($_POST['submitted'])){
$newcity = $_POST['city'];
setcookie("cookie_city", $newcity, time()+22896000);
die(header("Location: http://example.com/".$new_city));
}
if ((!isset($_COOKIE['cookie_city']))){
$city = "";
} else{
header("Location: http://example.com/".$_COOKIE['cookie_city']);
exit(0); // <<< Try this
}
That way, you'll have the cookie set and then user is redirected either way.
Do you still need that end block though? Are they always going to redirect to the city they selected?

Session variable gets lost between pages

I have numerous pages that I need to access a variable on. This variable is assigned a value when a user enters an ID into a form on accounts.php:
account.php
<form action="afterlog.php" class="form" method="post">
<input type="text" name="amid" id = "amid" class="input" />
<input class="btn" type="submit" value="Go" />
</form>
which posts the value 'amid' to afterlog.php
afterlog.php
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
header('Location: customer_view.php');
}
?>
which checks the POST, assigns the session variable, and redirects rhe user to customer_view.php
customer_view.php
I need to use '$account_manager_id' in this page and all pages after. Here is how I am assigning it the value of the _SESSION variable:
<?php
session_start();
$_SESSION['account_manager_id']=$account_manager_id;
?>
Bu the value isn't being held on any of the pages, including customer_view.php. I know its passing to afterload.php because it prints out on that page, but its gone after that page.
What I am doing wrong?
Thanks for your help!
You are trying to assign a value to $_SESSION['account_manager_id'] before $account_manager_id has any value in it. You just need to switch the order:
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
or simply:
$_SESSION['account_manager_id'] = $_POST['amid'];
in afterlog.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$account_manager_id = $_POST['amid'];
$_SESSION['account_manager_id']=$account_manager_id;
header('Location: customer_view.php');
}
?>
or
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$_POST['amid'];
header('Location: customer_view.php');
}
?>

Transfering the form data to more than one page using php

I have created one textfield and one dropdownlist and a button in my "index.php" page as follows:
<form value="indexform" action="" method="post">
<b>Number:</b><input type="text" name="number"/><br/>
<b>Network:</b>
<select name="network">
<option selected="">please...</option>
<option value="1">Bsnl</option>
<option value="2">Idea</option>
</select>
<input type="submit" name="proceed" value="submit">
</form>
and i have used the php code as follows for the above form:
<?php
if(isset($_POST['network']) && isset($_POST['number']))
{
$number = $_POST['number'];
$network = $_POST['network'];
}
if(!empty($_POST['network']))
{
switch($network)
{
case "1":header("Location:Bsnl.php");break;
case "2":header("Location:Idea.php");break;
default:exit();
}
?>
and i have crated two pages "Bsnl.php" and "Idea.php" Based on the selection taken in the dropdownlist the page will be redirected into either Bsnl/Idea.php
I have created the Bsnl/Idea.php page containg one textfield and submit button the code is as follows:
<form value="Bsnlform action="default.php" method="post">
<b>Amount:</b><input type="text" name="amount"/><br>
<input type="submit" value="submit">
</form>
php code is as follows:
<?php
if(!empty($_POST['amount']))
{
$amount = $_POST['amount'];
}
?>
atlast i want to print the entire form details(indexform as well as Bsnl/idea form) on the default.php page can any one help me how to do to print the values on default.php upto now i have tried the php code as follows:
<?php
echo $amount;
echo $network;
echo $number;
?>
First off, you need to enable session_start();
and change to this like so....
<?php
//Enable session first
session_start();
//Flush any existing session variables, you can choose where/if you wanna do this
session_unset();
if(isset($_POST['network']) && isset($_POST['number']))
{
$_SESSION['number'] = $_POST['number'];
$_SESSION['network'] = $_POST['network'];
}
if(!empty($_POST['network']))
{
switch($_SESSION['network'])
{
case "1":header("Location:Bsnl.php");break;
case "2":header("Location:Idea.php");break;
default:exit();
}
?>
Then change this to this....
<?php
//Session enable again
session_start();
if(!empty($_POST['amount']))
{
$_SESSION['amount'] = $_POST['amount'];
}
?>
Then in your next file....
<?php
session_start();
echo $_SESSION['amount'];
echo $_SESSION['network'];
echo $_SESSION['number'];
?>
Of course on your other page you wont echo $_SESSION['amount'], because it wont be set yet...

ECHO page1.php textarea values into page2.php

I have 2 php files in my folder. In page1.php, there's a textarea, user should enter some values in it. In page2.php, it will grab what is in the textarea and work with its program. But I can't find a command that grabs the value in textarea. Can someone help me?
page1.php:
<?
$hello = "hello";
?>
<html>
<input type = "text" name = "user_input">
</input>
</html>
page2.php
<?
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
?>
So, is there anyone that can solve this? =/
Use $_GET or $_POST in page2.php
page1.php
<?
$hello = "hello";
?>
<html>
<form method="get" action="page2.php" enctype="multipart/form-data">
<input type = "text" name = "user_input">
<input type="submit">
</form>
</html>
page2.php
<?
$text=$_GET['user_input'];
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
echo $text;
?>
You may use either $_GET['user_input'] or $_POST['user_input'].
The difference is, you can see the data in the url (visible to everyone) when using GET method and not in the other method.
Also, always use <input> elements (which you want to pass to another file) inside a <form> and specify action="file.php", to where you want to pass data, and the method, either method="get" or method="post", like;
<form method="get" action="page2.php">
also specify the method to grab data in the target file also, like;
$text=$_GET['user_input']; or $text=$_POST['user_input'];
And in your case, you may use;
Method 1
<?php
$hello = "hello";
?>
<html>
<form method="get" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_GET['user_input'];
echo $text;
?>
Method 2
<?php
$hello = "hello";
?>
<html>
<form method="post" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_POST['user_input'];
echo $text;
?>
If you want to share the data over a number of pages, you may do this using PHP Session or saving the data in a cookie.
1. Using Sessions
<?php
session_start();
$_SESSION['data'] = 1; // store session data
echo "Pageviews = ". $_SESSION['data']; //retrieve data
?>
Make sure you add session_start(); on every page you want to handle session
You can read more about php sessions here www.tizag.com/phpT/phpsessions.php/
2. Using Cookie
<?php
setcookie("user", "Alex Porter", time()+3600);
?>
and retreive it using
echo $_COOKIE["user"];
You can read more about php sessions here http://www.w3schools.com/php/php_cookies.asp
hope this helps...:)
basically your page1.php is a page with some form in it with a text area. Now user will have to fill it and submit the form to page2.php. You can't echo it's content like that, because that will be on browser subject to user actions. Use a form and submit the data to page2.php. Like this:
page1.php
<html>
<head>
</head>
<body>
<form action="page2.php" method="post">
<textarea name="t1">
</textarea>
</form>
</body>
</html>
page2.php
<?php
$textAreaContents = isset($_POST['t1'])?$_POST['t1']:'';
echo "You submitted: ".$textAreaContents;
?>
if i were you i should use sessions for this. that is where they were made for..
example:when user clicks on submit.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$_SESSION['post'] = $_POST;
}
that is where every post variable will be put in a session.
and your inputbox will be something like this..
<textarea name="message" type="text" value="" rows="0" cols="0" placeholder="" ><?php if(isset($_SESSION['post'])){echo $_SESSION['post']['message'];} ?></textarea>
?>
note that you now can use every post variable that you used in your form by echo (example)
echo $_SESSION['post']['message']
where message is the name of the inputbox. in this case of the textarea
don't forget that at the end when you don't want to use the session anymore use session_destroy(); otherwise you will keep having it in your form. and don't forget session_start(); above every page where you are planning to use sessions ( it must be at 1st line of your document at all times)

Categories