Passing php session variables non standard way - php

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.

Related

how to pass long value more then 65000 character in url without session

i want to pass long data value in next file using post method or in URL but it's say Submitted URI too large! how to solve this issue.
you can use $_SESSION
A session is a way to store information (in variables) to be used
across multiple pages. Unlike a cookie, the information is not stored
on the users computer. - W3Schools
Try this code using session
Index.php
<?php
//Index Page Storing variable
$_SESSION['var'] = $long_var;
?>
Action.php
<?php
//Action Page Getting session
$long_var= $_SESSION['var'];
?>
or use method POST/GET but add hidden input field just like this.
Index.php
<form method="POST" action="action.php">
<input type="hidden" name="var" value="long_var">
<input type="submit" name="submit">
</form>
Action.php
<?php
//getting POST variable from form
$long_var = $_POST['var'];
//getting GET variable from form
$long_var = $_GET['var'];
?>
use PHP_SELF with form action to send character in length more than 65000 characters.

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

HTML and PHP in one file

I'm a PHP newbie trying to sort some basics out. I have a user-form that leads to a mysql select query, which works fine. Every tutorial I have found so far has the standard form tag, ie: action='script.php' method='post'. This obviously opens script.php in a new tab/window though.
If I don't want to display what's fetched from my db on a different webpage I have to put the html and php in one document together. I didn't think this is how you would really want to do it though.
My specific question is when you want to display stuff on the same page do you just put everything in together within one document and let users hit the submit button?
NO you dont put your php scripts on the same page as your html file/s
Try this link for your reference =)
OR you can put 2 different pages that act as 1 by using INCLUDE FUNCTION
script1.php
<form action="script2.php" method="post" name="myform">
...
<input type="submit" name='submit_button' value="Submit" />
<input
</form>
---------------
script2.php
include 'script1.php';
if(isset($_POST['submit_button']
{.......}
Yeah You can put html and php in single document.
With the help of action.But it not the proper way.
In action you should mention this for writing html and php in same page.
<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>
You can use the same page as Action in form and make condition based on your submit button whthere it is pressed or not.
If it is pressed you can make your Code there for connecting db and do operation like select, insert, update or delete.
e.g.
Your file: script.php
<?php
if(isset($_POST['btnsubmit'])) {
// Do your Operation here...
}
?>
<form action="script.php" method="post" name="myform">
...
<input type="submit" name="btnsubmit" value="Submit" />
<input
</form>
What you can do is simply refer the user back to the form, or another page on your server with the header tag. Inside your PHP script you'd add something similar after your query executes correctly
header( 'Location: ' . $_SERVER['HTTP_REFERER'] ); // Refer to the last page user was on...
Or another URI
header( 'Location: http://some.url/' );
If you really want to do this, here is a way:
<?php
if(isset($_POST)){
//do your php work here
}
?>
<html>
<form method='POST'>
//form elements here
<input type='submit'>
</form>
<!-- other html code -->
</html>
It depends on the length of your code, if the code is too much, then the better way is to include some script file to your parent file. using include() functions, and your perfect answer is yes. just put everything in together within one document

Form Input to Session Variable

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

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.

Categories