Saving form information with sessions - php

I'm trying to use sessions to save form input, but it works only on the last form submitted.
I have an index.php that goes like this:
<?php
session_start();
$_SESSION['number-of-sessions'] = 0; ?>
...
then, there's a link to form.php:
...
<form action='display.php' method='post'>
<input type='text' name='name'/>
<input type='text' name='id'/>
<input type='submit' value="Submit"/>
...
And here's display.php:
<?php
session_start();
$_SESSION['number-of-sessions']++;
$_SESSION[$_SESSION['number-of-sessions']] = $_POST;
?>
...
<?php
for($i = 1; $i <= $_SESSION['number-of-sessions']; $i++) {
print_r($_SESSION[$i]));
}
?>
// another link that goes back to form.php
So basically, it always prints the last form submitted and the others are just blank space.
Is there any other way of doing this or am i doing it right?
p.s: i can't use databases.

Not surprisingly, you have a new $_SESSION for each session, so stocking any information that way won't work.
If you want to persist data across sessions, you have to persist it somewhere. Consider writing it to a file if you can't use databases, for example.

Related

Php POST method to 2 page

I just sent data to a page called diak_o.php with post method but I need to use this data on an another page. How can I send it to two pages or send from the first page to the next?
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" />
</form>
You could store it in Sessions and access it on multiple pages like this:
Page 1:
<form action="page2.php" method="post">
<input type="text" name="page1text"/>
<input type="submit"/>
</form>
Page 2:
<?php
session_start();
$dataFromPage1 = $_SESSION['page1text'] = $_POST['page1text'];
echo $dataFromPage1;
?>
You can use $_SESSION or just but i think $_POST should still work in the next file too...
when you send that data from this page to second page like diak_o.php in this page you can access it by below code.
in diak_o.php write code like below.
<?PHP
session_start();
echo $_POST['name'];
$_SESSION["name"] = $_POST['name'];
?>
in the other page you can use $_SESSION["name"] by accessing it.
you can also use COOKIE OF PHP.
On this URL there are different methods for passing data from one page to another.
http://www.discussdesk.com/how-to-get-data-from-one-page-to-another-page-in-php.htm
Thanks.
You need to give your button a name attribute, then on diak_o.php you check if the button isset, after that you check if the text input is not empty, else assign a session to the text input
Your Form
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" name="submit" />
</form>
diak_o.php
<?php
session_start();
if(isset($_POST['submit'])){
if(empty($_POST['name'])){
die("enter name");
}else{
$_SESSION['name']= $_POST['name'];
}
}
?>
anotherpage.php
<?php
session_start();
echo $_SESSION['name']; // will output the value from form.
?>
when ever your wanna use the value on your pages, just use $_SESSION['name'];

PHP - store input variable for static reuse

There are some inputs, and there is a function. The function requires these inputs, and the inputs are user-given. But, the buttons that fire the function and the input submission form are two different buttons. So, when the user presses "submit" to store his variables, the variables are stored fine. But, when he presses the "calculate" button (which fires the function), php says "undefined index" because it reads the $_POST of that input again and again.
If I disable register_globals, it does not show 'undefined index' but these values are 0 again.
If I use another file just to store these values and then redirect back to the page where the function button is, there is a redirect loop, require_once does not work.
What is the way to store the inputs in such way that they can be used again and again in functions and whatsoever? No databases, I need a way to store them in variables.
edit: the form: <label for="asdf">enter value:</label> <input type="text" id="asdf" name="asdf" value="<?php echo $asdf;?>" />
storing the value:
$asdf=$_POST['asdf'];
then I need to write $asdf in the function with the updated value that the user gave through the html form. How to do it? Cannot be much simpler
I would just store them in the session. That way they, they can be used across php scripts, but are not stored in the long-term. Here's an example:
form.php
<?php
session_start();
?>
<html>
<body>
<form action="store.php">
<input type="text" name="x" value="<?php echo $_SESSION['x'] ?>">
<input type="text" name="y" value="<?php echo $_SESSION['y'] ?>">
<input type="submit" value="Submit">
</form>
<form action="calculate.php">
<input type="submit" value="Submit">
</form>
</body>
</html>
store.php
<?php
// Start the session
session_start();
$_SESSION["x"] = $_POST['x']; // substitute your input here
$_SESSION["y"] = $_POST['y']; // substitute your input here
?>
calculate.php
<?php
// Start the session
session_start();
$result = $_SESSION["x"] * $_SESSION["y"];
echo $result;
?>
There is no way to store them in variables. Every request to your server is a new request.
You could store the variables in a cookie/session or give them back after pushing the first button and store them in a hidden field in your html form. Or store them in a file on your server.

How to store a radio button in a session for use on another page?

So I need to store the choice of selected radio button in session and then based on that value perform an action on a different page.
Page1.php:
<input type="radio" name="person" value="p1"/>Person1
<input type="radio" name="person" value="p2"/>Person2
Page2.php
if Person1 is selected on page one
//do this
if Person2 is selected one page two
//do this
First, you will have to start a session in Page2.php
start by calling (at the top of your page)
<?php
session_start();
//post your data
$person = $_POST['person'];
$_SESSION['person'] = $person;
?>
You will have to have the inputs be wrapped inside of a form:
<form method="post" action="Page2.php">
<input type="radio" name="person" value="p1"/>Person1
<input type="radio" name="person" value="p2"/>Person2
<input type="submit" value="submit"/>
</form>
Now you will be able to use that $_SESSION variable on mostly any page, if the session is not destroyed or overwritten.
To retrieve a session value on another page, simply use:
<?php
session_start();
$person = $_SESSION['person'];
?>
You can do this by posting the data in your form through to your Page2.php page you don't need a session.
$_POST["person"];
Will pull the data out so that you can do:
string person = $_POST["person"]
if(person == something){
//do something
}
else{
//do something else
}
You only need a session if you intend to use the variable value that is returned on multiple occasions. If that's the case then you can find a good easy tutorial here:
http://www.w3schools.com/php/php_sessions.asp
The basics would look something like this within your Page2.php:
session_start();
$_SESSION["person"] = $_POST["person"];
Hope that helps!

Pass variable from one page to another, then discard it

Is there a way I can pass a variable from one page to another but it only work on the next page clicked? After that it can be discarded/destroyed.
I've tried a php session but can't seem to kill the session on the next page clicked... or even if that way may be the wrong way to approach it.
Here's my session code:
<?php
session_start();
$x = $category;
$_SESSION['sessionVar'] = $x;
echo "$x";
?>
<?php
session_start();
$x = $_SESSION['sessionVar'];
echo "$x";
?>
I want to do this without having to submit a form.
You can pass a variable via a php session and then unset the variable on the following page.
$_SESSION['my_var'] = "some data";
$_SESSION['my_var'] = null;
unset($_SESSION['my_var']);
Send it as POST:
<form action="nextpage.php" method="post">
<input type="hidden" name="yourvariable" value="12345" />
<input type="submit" name="submit" value="Next page" />
</form>
Is it what was supposed?

Reload page, keep sessions

I'm trying to develop a function where a user can click "Previos week"/"Next week" and that will trigger some $_SESSION variables to change values (dates).
I have this code on absence.php (I know that PRG pattern is not implemented. I have no idea on how to do this but still keep functionality).
<?php
if (isset($_POST['decrease_date'])) {
$monday_value = ($_SESSION['sess_mon'] = strftime("%Y-%m-%d", strtotime("{$_SESSION['sess_mon']} -1 day")) );
}
if (isset($_POST['increase_date'])) {
$monday_value = ($_SESSION['sess_mon'] = strftime("%Y-%m-%d", strtotime("{$_SESSION['sess_mon']} +1 day")) );
}
else {
$monday_value = ($_SESSION['sess_mon'] = date('Y-m-d', strtotime('Monday this week')) );
}
?>
<form action='absence.php' method='post'>
<input type="hidden" name="decrease_date"/>
<input type='submit' value='Previous'>
</form>
<form action='absence.php' method='post'>
<input type="hidden" name="increase_date"/>
<input type='submit' value='Next'>
</form>
Now I would like to reload the page (with jQuery location.reload()-function) but still keep the $_SESSIONs what was active at the moment before the page was refreshed. Currently this will "reset" since there is no $_POST.
Ideally I would like to send the <form action'#'> to another page. However when you first enter absence.php the dates should be reset.
How would I do this?
change
session_start();
into:
if (!isset($_SESSION)){
session_start();
}
Instead of reloading, can't you just submit form by jquery $("form#frmDate").submit(); Don't forget to give ID to your form frmDate.
Is this what you are looking for?

Categories