Hidden variable and session - php

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.....
}
?>

Related

Can't use a form's value in a different php file

Having issues with using a form's value in a different php file:
my firstpage.php
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
</form>
my secondpage.php is here
<?php
include("firstpage.php");
$result = $_POST['rdbbtn'];
if ($result == "1") {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
problem:
Notice: Undefined index: rdbbtn in
how come I can't use "rdbbtn"? Should I have something like
$rdbbtn = $_POST['rdbbtn'];
in secondpage.php? Tried this but didn't solve my problem.
firstpage.php and secondpage.php are in the same directory.
Probably it's some pretty obvious thing that I don't see...thanks!
EDIT: I have accepted pradeep's answer as that helped me the most to figure what the problem should be. would like to say thank you for everybody else showing up here and trying to help!
When you change current page it reset the value and $_POST is empty.
You can try with set form action to next page . It will work
<form method="post" action="secondpage.php">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="" value="Next">
</form>
Other wise you can make a function in a class and set each page action
to this function.
And set your each form data to session.
Finally when you change the page you read data form session.
Class FormAction{
public function setFormDataToSession(){
if(isset($_POST['rdbbtn']){
$_SESSION['rdbbtn'] = $_POST['rdbbtn'];
}
}
}
In your page simply get the session value.
echo $_SESSION['rdbbtn'];
Should be like this :
Check with isset method in
<?php
include("firstpage.php");
$result = isset($_POST['rdbbtn']) ? $_POST['rdbbtn'] : NULL;
if ($result == 1) {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
and your form should be like this :
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="submit" value="submit">
</form>
Sorry for not being able to comment in this post(less reputations). But seems like you are asking about storing the variables of the session. This way you can use the variables for a whole session. Just start the session by putting session_start() in the very beginning of secondpage.php file and then you can access the variables at any time during the session by simply calling $_SESSION['rdbutton] in any page like fourthpage.php or anything. Just make sure u put the session_start() at the top of each page where you want to use the variables. Don't forget the semicolons at the end. 😜 Hope this helps.

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()

Send form data to the same page not functionning

I'm a newbie in PHP, and I would like to send datas from a form and display it into the same page, here is my code for better understanding:
<form method="post" action="same_page.php">
<input type="text" name="owner" />
<input type="submit" value="Validate" />
</form>
<?php
if(isset($_GET['owner']))
{
echo "data sent !";
}
?>
So normally, after having entered some random text in the form and click "validate", the message "data sent!" Should be displayed on the page. I guess I missed something, but I can't figure out what.
You forgot to add submit name in your form.You are using POST as method so code should be
<form method="post" action="">
<input type="text" name="owner" />
<input type="submit" name="submit_value" value="Validate" />
</form>
<?php
if(isset($_POST['submit_value']))
{
echo '<pre>';
print_r($_POST);
}
?>
Will display your post values
You are using a POST method in your form.
<form method="post" action="same_page.php">
So, change your code to:
if (count($_POST) && isset($_POST['owner']))
Technically, the above code does the following:
First checks if there are content in POST.
Then, it checks if the owner is set.
If both the conditions are satisfied, it displays the message.
You can actually get rid of action="same_page.php" as if you omit it, you will post to the same page.
Note: This is a worst method of programming, which you need to change.
You should Replace $_GET['owner'] with $_POST['owner'] as in your form you have specified method='post'
Replace:
$_GET['owner']
With:
$_POST['owner']
Since you are using the post method in your form, you have to check against the $_POST array in your PHP code.

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

Add data to sql on button click

my page receives data which i retrieve with $_post. I display some data and at the bottom of page my button has to save data to mysql. I could submit form to next page, but how do i access the data that I have retrieved with post then? Lets say i have following code (in reality alot more variables ..):
<?php
$v= $_POST["something"];
echo $v;
echo "Is the following information correct? //this would be at the bottom of the page with the buttons
?>
<input type="button" value="submit data" name="addtosql">
You can do it in two methods:
1) You can save the POST variable in a hidden field.
<input type="hidden" name="somevalue" value="<?php if(isset($_POST["something"])) echo $_POST["something"];?>" >
The hidden value also will get passed to the action page on FORM submission. In that page you can access this value using
echo $_POST['somevalue'];
2) Use SESSION
You can store the value in SESSION and can access in any other page.
$v= $_POST["something"];
session_start();
$_SESSION['somevalue']=$v;
and in next page access SESSION variable using,
session_start();
if(isset($_SESSION['somevalue']))
echo $_SESSION['somevalue'];
Take a look. Below every thing should be on single php page
// first create a function
function getValue($key){
if(isset($_POST[$key]))
return $_POST[$key];
else
return "";
}
// process your form here
if(isset($_POST['first_name']){
// do your sql stuff here.
}
// now in html
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="first_name" value="<?php echo getValue("first_name"); ?>" />
<input type="submit" />
</form>

Categories