Form Input to Session Variable - php

First question I have is that I would like on the index.php to ask the user a question via a form and when the press submit it updates the session variable on jcart.php. With the current code below when I call the session variable later on it is now found so I assume the code I have just now is not working correctly.
The second question is when I press submit it takes me to jcart.php is there a way to avoid this or have it go back.
On my index.php I have a form :
<form action="jcart/jcart.php" method="post">
<input type="text" name="example" id="example" />
<input type="submit" name="submit" value="Submit" />
</form>
And on Jcart.php :
$_SESSION['example'] = $_POST['example'];
Then on the page I am calling it on cocktails.php
<?php
include_once('jcart/jcart.php');
session_start();
?>
<input type="hidden" name="my-item-id" value="<?php echo $_SESSION['example'];?>" />
Thanks for your help.

There is no need to "update the session variable on jcart.php". Once you store a data into the global $_SESSION array, it should be available on all php files, at least until you destroy the session.
That being said, if jcart/jcart.php needs to have a $_SESSION['example'] variable, you need to be sure, that the session is started before including the file, for instance:
<?php
session_start()
include_once('jcart/jcart.php');
?>
For your other question, you can change the action inside your form to whatever you like or issue a header('Location: /'); to redirect to other page after the value was recieved.

Please try this
*jcart.php*
session_start();
$_SESSION['example'] = $_POST['example'];
*then cocktails.php*
include_once('jcart/jcart.php');
echo $_SESSION['example'];

in jcart/jcart.php
session_start();
should be called at the beginning

Related

form permanently write variable

So when the user clicks submit, I would like it to take the form inputs and permanently save them to a variable. I'm not to sure how this could be done, but I am aware of this method, but it doesn't save it permanently.
<?php
$test = %_POST["example"];
?>
<form action="#" method="post">
Example Input: <input type="text" name="example"><br>
<input type="submit" name="submit"><br>
</form>
I then put
<?php echo $test ?>
which displayed my variable value, but as soon as the page is refreshed it's gone because of POST. How can I do something similar but when the page is refreshed it's still there?
I am open to other alternatives.
The problem is that $_POST variable lives only "per request", as you have already seen yourself when refreshing the page.
You can however use sessions to keep the variable alive as long as the session lives. Or you save the data to a database and fetch the data again when requesting the page.
Regarding sessions, you would do that like this:
<?php
session_start();
if (isset($_POST['example'])) {
$test = $_POST["example"];
$_SESSION['formData'] = $test;
}
if (isset($_SESSION['formData'])) {
echo $_SESSION['formData'];
}
<?php
For more information and a simple tutorial see: http://www.w3schools.com/php/php_sessions.asp

POST values being lost from included page preventing session data from being created

I have a main page including a header page to make editing the header across all pages a quicker process. The issue I am running into is that this header has search option on it, and I want the search term to be saved in a session variable. It works fine if the header is on the page itself and not on an included page, but as soon as I put the header into its own file the search function stops working as intended.
this is simply the include code:
<?php
if (!isset($_SESSION)) {
session_start();
}
$redirectAction = $_SERVER['PHP_SELF'];
?>
<html>
<body>
<?php include 'http://www.saxon564.com/tclusa/header.php'; ?>
</body>
</html>
Here is the header.php code:
<?php
if (!isset($_SESSION)) {
session_start();
}
if ((isset($_POST["search"])) && ($_POST["search"] == "1")) {
$_SESSION['query'] = $_POST['query'];
}
?>
<html>
<body>
<form action="<?php echo $redirectAction; ?>" method="POST">
<input type="text" name="query" id="query" placeholder="Search" size="20" value="<?php echo $_SESSION['query']; ?>" />
<input type="hidden" name="search" id="search" value="1" />
<input type="submit" class="sbmt" value="Search" />
</form>
</body>
</html>
What happens is when I submit the search, the page reloads, but the search data was not saved. I am probably missing something important, but I am getting no where with this. Does anyone have a thought as to what the issue is, or if this just isn't possible?
Dont use absolute path in include. You have to use relative path to get it work.
<?php include '/tclusa/header.php'; ?>
$_SESSION is always set as a PHP Global variable. I don't think you would need to check if the session exists before starting it. And also you can't start the session twice per request (so just remove if from the included header file). You should do a session_start() once at the very beginning of your application and thats it.
If you really do need to check if the session has been initialized use empty(&$var).

Passing php session variables non standard way

I use the following PHP code and HTML form to pass the "item_name" value to downloading page.
<?php
session_start();
$_SESSION['item_name']="item_name";
?>
...
<form action="download.php" method="post">
<input type="hidden" name="item_name" value="133076">
<input type="submit" value="download">
</form>
I receive and use the "item_name" value on downloading page like this.
<?php
session_start();
$item_name=$_POST["item_name"];
?>
All this works fine, but I have the following problem that I hope to solve here. After initial click I want to come back to the same html page like this:
<form action="" method="post">
then do something using JavaScript, generate the link to "download.php" and after that be able to download the file. The problem is that by the time when I'm clicking the newly generated link to go to "download.php" the
$_SESSION['item_name']="item_name";
is already lost. So how I can preserve this Session variable to use it on download page?
Thanks.
On your download.php page, you need to set the variable as a session variable:
<?php
session_start();
$_SESSION['item_name'] = $_POST["item_name"];
?>
By doing the above, the variable will be stored in the PHP session and will be available wherever you call session_start().
The way you have it right now is:
<?php
session_start();
$item_name = $_POST["item_name"];
?>
which will make the variable available only within your download.php page and nowhere else.

php, echo doesn't show after form post?

i have a form and when i post it i create a cookie, then i read the cookie and if isset then do something:
inside read.php
<?php if (isset($_COOKIE['voteforme'])) {
echo 'You voted this profile';
} else {?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="vote_points" id="vote_points" value="1000" />
<input type="submit" name="votes_send" id="votes_send" value="Vote for me" />
</form>
<?php } ?>
then i do some cookie creation inside 'create.php':
if (isset($_POST['votes_send'])){
$get_vote_for_me = $_POST['vote_points'];
$get_talent_id = $_POST['talent_id'];
$value1 = "voteforme";
$value2 = "voteforme_points";
setcookie($value1,$value2, time()+3600*24*7);
}
this script is creating the cookie i need. basically if the cookie $_COOKIE['voteforme'] is set then show a message, else show the form.
the problem i have is that i need to refresh the page a second time for the page to read the cookie and see if exists or not.
the file structure is index.php where i include the read.php and 'create.php'
any ideas??
thanks
edit:
even if i set the form action to any of those files the result is the same
edit, index.php structure:
<?php
require_once("read.php");
include 'create.php';
?>
<!doctype html>
<head>...
<body>...
<div id="tab5" class="tab_content">
<?php read();?> // the read.php it's a function
</div>
...
the read.php i am requiring it it at the top but i'm not actually calling git until inside the body as a function
adit:
i've also tried to add the setcookie inside the else statement inside the 'read.php', but there it doesn't get created
Why don't you check for the cookie set after you set the cookie? Then proceed to update your page. You just can't print anything to the page before doing your cookie work.
Another solution would be to use javascript to reduce the number of reloads.
A third idea would be to use a global variable which you can check in addition to the cookie -- that way if you set the cookie you would execute the appropriate code based on the global variable.
I would suggest you separate the files. POST data to create.php and add a redirection in create.php back to index.php. This way page will load and it will be able to read your cookie.
or you could try this, if not done already:
first include create.php and then read.php
setcookie() doesn't set the value in $_COOKIE immediately, it'll only be there after the client sends another request with the cookie. You can set it manually if you want.

How to destroy the php session with one button

I'd like to make a simple form button which completely destroys the session when you click on it. I am just starting to use PHP for the first time, and do not see how I implement it into my HTML code.
What I'd like is simply a form button which will clear the session (and possibly an explanation as to how it works)
The form button is just like any other form button, nothing special. Catch the POST on the php side of things, and use session_destroy(); to kill the session data entirely.
See this guide for info about forms and post if you're hazy on the subject: http://www.tizag.com/phpT/postget.php and this http://www.tizag.com/phpT/phpsessions.php for info about sessions
More info about forms and PHP and how to work with the data from the form: http://www.tizag.com/phpT/forms.php
Example:
Order.html:
<html><body>
<h4>Tizag Art Supply Order Form</h4>
<form action="process.php" method="post">
<input type="submit" />
</form>
</body></html>
process.php:
<html><body>
<?php
session_destroy();
?>
</body></html>
It's cheesy...does this help?
index.php
<?php session_start(); ?>
<form action="clear-session.php" method="POST">
<input type="submit" value="Clear session" />
</form>
clear-session.php
session_start();
session_destroy();
header('Location: index.php');
exit();

Categories