php transfer variables from page to another pages - php

i have 4 pages like below:
page1
session_start();
<input type="text" name="log1" value="".$_SESSION["log1"]."">
page2
session_start();
$_SESSION["log1"] = $_POST["log1"]
<input type="text" name="log2" value="".$_SESSION["log2"]."">
page3
session_start()
$_SESSION["log2"] = $_POST["log2"]
<input type="text" name="log2" value="".$_SESSION["log3"]."">
page4
session_start();
$_SESSION["log3"] = $_POST["log3"]
<input type="text" name="log2" value="".$_SESSION["log4"]."">
i transfer with method post in order page1 > page2 > page3 > page4 . if i return to page2, i not have $_SESSION["log2"] in value of input And displays it an empty field.
Where is the problem?

You're using bad syntax for using PHP with HTML. Instead of:
session_start();
$_SESSION["log1"] = $_POST["log1"]
<input type="text" name="log2" value="".$_SESSION["log2"]."">
Use:
<?php
session_start();
$_SESSION["log1"] = $_POST["log1"];
?>
<input type="text" name="log2" value="<?php echo $_SESSION["log2"]; ?>">
Also make sure to use form to post data to another page to set session variables.

Related

PHP - How to pass value in database to another page when clicked

how can I pass this value which data is in database. I want to pass the value from another page by textbox. how can I do it?
here is my database
and I want to pass the Value on the textbox from another page how can I do that?
here is my code
include("topupcard.php";)
<td>
<?php echo htmlentities($row['value']);?>
</td>
<button class="btn btn-success mr-4">Generate QR Code</button>
and to pass the data to another page.
<input type="text" value="$row['value']" name="id">
right now, it is not working nothing is being passed.
also, i dont know if I'll use Session or not, help thanks.
so the output should be 200 or 1000 is displayed on the other page when i click the button.
Using #danblack's explanation you can
Option 1 (not a safe method if this involves transactions):
<a href="generatecode.php?value=<?php echo htmlentities($row['value']); ?>">
then on the other page
<input type="text" value="<?php echo $_GET['value']; ?>" name="id">
Option 2 :
session_start();
$_SESSION['value'] = $row['value'];
then on the other page
session_start();
$value = $_SESSION['value'];
<input type="text" value="<?php echo $value; ?>" name="id">

Hidden variable and session

I'm trying to get the value of a hidden input
but when I try this echo $out_1; I see nothing.
I think that I have committed some basic error but I can't find it.
page n°1-.php
<form action="https://www.coinpayments.net/index.php" method="POST">
<input type="hidden" name="item_number" value="article_1">
<input type="hidden" name="currency" value="USD
<input type="hidden" name="amountf" value="5.10000000">
<input type="image" name="ordered" src="https://www.coinpayments.net/images/pub/CP-main-large.png" alt="CoinPayments.net">
</form>
<?php
if(isset($_POST['ordered_x'], $_POST['ordered_y']))
{
session_start();
$out_1= $_POST['item_number'];
$_session['item_number']= $_out_1;
}
page n°2--.php
<?php
session_start();
$ouput= $_session['item_number'];
echo "$ouput"; // it shows nothing when i try this
?>
When I try echo $out_1; in the first page to see what happens, it shows nothing again.
You haven't started session in second page.
put session_start() function
you also have to define html control for ordered_x and ordered_y in html form so php can post values for that control and after that your condition will become true.
<?php
if(isset($_POST['ordered_x'], $_POST['ordered_y']))
{
session_start();
code.....
}
?>

How do i make form variable to display on all my site pages?

This is my form:
<form id="form1" name="form1" method="post" action="tracking.php">
<label>
<input type="text" name="trckno_trk" id="trckno_trk" />
</label>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</form>
When i submit my form, the submitted form variable displays well on the "tracking.php" page using <?php echo $_POST['trckno_trk']; ?>.
But when i click on other pages in the site, it doesn't seem to display. That mean that the form variable echo $_POST['trckno_trk'] displays only on one page but does not display on any other page.
Please, how can i get it to display on every other page on my site.
Try this
<?php
// Start the session
session_start();
?>
Then you html
<form id="form1" name="form1" method="post" action="tracking.php">
<label>
<input type="text" name="trckno_trk" id="trckno_trk" />
</label>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</form>
Then you should save the variable in your Session
<?php echo $_POST['trckno_trk'];
$_SESSION["trckno_trk"] = $_POST['trckno_trk'];
?>
Now you can use this Session to display on other pages.
Form submited data are available only for page you specified in action parameter. They are not stored in any way. You need to save them for example in database or SESSION variables and retrive them again when accessing other pages.
Form parameters are accesible only when you post them, to use them later you will need to save them somewhere.
If you'd like to save the value & display it everywhere for all clients, you will need to store it in a database (e.g. MySQL), fetch the value and print it
If you'd like it to be unique for each client, you could use cookies, for example:
setcookie('cookiename',
'the value',
time()+86400 /* seconds until it expires */,
'/' /* On what pages do you want to use it, '/' means all pages*/,
);
The code above saves the cookie, to print its value:
echo $_COOKIE['cookiename'];
Another option is to save it in the session, but it will expire after a short time
session_start(); // put this on the start of every page to start the session
$_SESSION['name'] = 'value'; // save a value in the session
echo $_SESSION['name']; // print it
I suggest you use $_SESSION[] to store your POSTED value, this way you will be able to access it on any other page.
So on your tracking.php you can have code like this
session_start();
$_SESSION['trckno_trk']=$_POST['trckno_trk'];
then on other pages you can access the variable using
session_start();
echo $_SESSION['trckno_trk'];

Dropdown selected value in a SESSION variable accross different pages doesn't persist

On my list.php page, the user can choose a country from the drop-down to show the SQL server's data for the selection. On this selection, the user gets the option to update/edit the values on an edit.php page. When the user submits the changes on the edit.php page, they are redirected to a save.php page and then back to the initial list.php page. I was wondering if it's possible to use the SESSION variable to store the initial country selection so the user gets the same selection when redirected to list.php. I've tried to use session_start ();, but couldn't get it to work.
EDITED
This are pieces of my code:
Top of list.php:
<?php session_start(); ?>
Form:
<form name="frmname" id="frmid" action="list.php" method="POST" >
<h4>From: <input type="text" size="6" name="start_date" pattern="[0-9/]+"
placeholder=" 00/00/0000" />
To: <input type="text" size="6" name="end_date" pattern="[0-9/]+"
placeholder=" 00/00/0000" />
<select name="RegioSelect" onchange="this.form.submit();" ><option><?php echo $_SESSION['RegioSelect']; ?></option>
Dropdown populated by:
$query1 = "SELECT DISTINCT CountryRegionName FROM tKein23 ORDER BY CountryRegionName";
$stmt1 = $conn->query( $query1 );
while ($data = $stmt1->fetch(PDO::FETCH_ASSOC)){
echo '<option value="'.$data['CountryRegionName'].'">';
echo $data['CountryRegionName'];
echo "</option>";
}
Then I tried SESSION_START():
<h4><input type="submit" value="Show" name="Selection" />
</select>
</form>
<?php
if(isset($_POST["RegioSelect"])){
$_SESSION['RegioSelect'] = $_POST["RegioSelect"];
}
Now, on other pages, I do get the $_SESSION['RegioSelect'] variable. On list.php, I get the variable in the drop-down but no submission.
session_start() must come first, before any other ouput. Not only before html on your php script, but before any browser output.
PHP Documentation
To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
<?php session_start(); ?>
<html>
<body>
...
...
<h4><input type="submit" value="Show" name="Selection" />
</select>
</form>
<?php
if (isset($_POST["RegioSelect"]))
$_SESSION['RegioSelect'] = $_POST["RegioSelect"];
If the form is on the same page, make sure to move the
if (isset($_POST["RegioSelect"]))
$_SESSION['RegioSelect'] = $_POST["RegioSelect"];
to the top - right below the session_start() and before the <html>
otherwise you have to reload the page first - which is probably not what you want
from there on you can also do redirects to other pages via header()

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'];

Categories