Carry POST data to another page - php

I have a php form that at the moment posts the data to a Paypal cart, but I want to post the data to a new php page almost like a confirmation page which shows the options and selections you selected to and from that point for you to be able to send on to Paypal?
Is this possible?

Someone new to session or post may find this example helpful. complete example of what you want. how to Store and view post array into a session named POSTDATA.post or anyother variables are accessible to a same session, from any page if its stored in a session variable.
form.php
<?php
//PHP content of form.php file
session_start();
if ($_POST)
{
$_SESSION['POSTDATA']=$_POST;
}
?>
<html>
<head></head>
<body>
<form action="form.php" method="post">
<input type="text" name="hello"/>
<input type="submit" name="submit" value="submit"/>
</form>
<!--link to some another page for testing-->
</body>
</html>
anotherpage.php
<?php
session_start();
echo "This is some another page somewhere inside.ANOTHER PAGE SAYS";
echo '<pre>';
print_r($_SESSION['POSTDATA']);
echo '<pre>';
echo "INPUT BOX VALUE ".$_SESSION['POSTDATA']['hello'].'<br/>';
echo "SUBMIT BUTTON VALUE ".$_SESSION['POSTDATA']['submit'];
?>

One possibility is to store the post data in $_session['post'] = $_POST to persist the storage and get the posted data on the new page with $_session['post']['field']

Related

HTML Button that saves and move to the next page

I have a small problem. I want to have a button in my html page that saves every data that is added in the textfields and also when I click it to move to the next page.
My code is the follow...
<input type=button onClick="location.href='education.php'" value='Next'>
but it only moves to next page it does not save the data in the database ...
Can you help me please?
Thanks.
Remove the JavaScript
Change the type to submit
Wrap it in a <form>
Set the action of the form to education.php
Set the method of the form to post
Then, in education.php, read the data from $_POST and use PDO (with bound variables) to insert it into the database.
Try this :
<?php
if(isset($_POST['submit']))
{
// Insert Query Put here
header('Location: education.php');
}
?>
<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" value="Next" name="submit">
</form>
</body>
</html>
education.php :
<?php
echo "Successfully Updated.";
?>
You will have to set an action to your form like below because you are not submitting the form, but just redirection to another page without taking the form data.
<form action="education.php" method="post">
<!-- All your input fields here -->
<input type="submit" name="submit" value="Next">
</form>
and your education.php should be look like this:
<?php
//Get all parameters using $_POST
//Make A connection to database
//Choose a database in which you have to save the data
//Create a SQL query
// run query using mysql_query($query);
//Redirect to anywhere with header("Location:page.php");
?>

Can't seem to pass SESSION variables of POST

Sorry if this is a duplicate, but I really cant find anything that could solve my problem. I can pass numbers and strings like $_SESSION['blabla']="123'; but I can't pass this $_POST value from the textfield and submit button.
Page 1 (sessions.php)
<?php session_start(); ?>
!doctype stuff here
<body>
<form id="form1" name="form1" method="post" action="sessions2.php">
<label>
<input type="text" name="damn" id="damn" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</form>
<?php
$omg = $_POST['damn'];
$_SESSION['damn'] = $omg;
echo $_SESSION['damn'] ;
?>
Page 2 (sessions2.php)
<?php
session_start();
$fires = $_SESSION['damn'];
echo "wth";
echo $_SESSION['damn'];
?>
PS. Sorry for the names.. I'm truly stumped.
You need to put the code that reads from $_POST in the file that you submit the form to.
Currently your process is:
Get request for sessions.php
Send form to browser
Assign $_POST['damn'] (which is undefined) to the session.
User submits form
Get request for sessions2.php
Ignore $_POST (which is now populated)
Read from the session (where the variable is still undefined).
damn is populated in the form submission request (step 4/5) not the request where you are trying to read it (step 1).
In sessions2.php
// you POST "damn" variable via form, using post method, so:
$fires = $_POST['damn'];
// and:
$_SESSION['damn'] = $fier;
// or
$_SESSION['damn'] = $_POST['damn'];
PHP code in file sessions.php doesn't work, because in form action you have session2.php.

make a form action to another page without losing the information

I want to make a form which takes information then uses that information on another page when it is submitted. However once it redirects, it loses all the information from the other page for example:
Page 1:
<?php
if(isset($_POST['submit']))
{$info=$_POST['info'];}
?>
<html>
<form action='page2.html' method='POST'>
<input name='info'>
<intput type='submit' name='submit'>
</form>
</html>
Page2:
<?php
echo $info;
?>
it doesn't know what the variable 'info' is on page 2.
Add:
if(isset($_POST['submit']))
{ echo $_POST['info'];}
and remove:
echo $info;
Now reasons:
When you submit a form it's redirected to a page written in action attribute of form tag and sends form data to it. So after submitting form you are on the page2 where you have access to posted data.
If you are posting to page2.html then your form data will be in the global $_POST variable. Try
print_r($_POST);
in your page2.html php
in page 2:
echo $_POST['info'];
I don't know why you have the variables $_POST in the first page. Also you should name the page2 page2.php unless you have configured it otherwise in your web server.
Page 1:
<html>
<form action='page2.html' method='POST'>
<input name='info'>
<intput type='submit' name='submit'>
</form>
</html>
Page 2:
echo $_POST['info'];
Or, to see everything that's passed between pages:
print_r($_POST);

how to pass value from one php page to another using session

I can pass values form one page to another but I need to pass value like this,
Page 1:
Page4.php
Page3.php
I need to pass the value in a text field in the Page1.php to a text field in Page2.php, since the form is not directly redirectly to page2, I am unable to pass the value, I tried session, form post method and few other methods but I am yet to succeed.
I would be very happy if you can help me with the code or some suggestions.
Thanks!
Edit..........
I found the answer, thanks for the help, it was actually a careless mistake on my part, I used $_post instead of $_session.
Its working now.
Thanks for the help.
Use something like this:
page1.php
<?php
session_start();
$_SESSION['myValue']=3; // You can set the value however you like.
?>
Any other PHP page:
<?php
session_start();
echo $_SESSION['myValue'];
?>
A few notes to keep in mind though: You need to call session_start() BEFORE any output, HTML, echos - even whitespace.
You can keep changing the value in the session - but it will only be able to be used after the first page - meaning if you set it in page 1, you will not be able to use it until you get to another page or refresh the page.
The setting of the variable itself can be done in one of a number of ways:
$_SESSION['myValue']=1;
$_SESSION['myValue']=$var;
$_SESSION['myValue']=$_GET['YourFormElement'];
And if you want to check if the variable is set before getting a potential error, use something like this:
if(!empty($_SESSION['myValue'])
{
echo $_SESSION['myValue'];
}
else
{
echo "Session not set yet.";
}
Solution using just POST - no $_SESSION
page1.php
<form action="page2.php" method="post">
<textarea name="textarea1" id="textarea1"></textarea><br />
<input type="submit" value="submit" />
</form>
page2.php
<?php
// this page outputs the contents of the textarea if posted
$textarea1 = ""; // set var to avoid errors
if(isset($_POST['textarea1'])){
$textarea1 = $_POST['textarea1']
}
?>
<textarea><?php echo $textarea1;?></textarea>
Solution using $_SESSION and POST
page1.php
<?php
session_start(); // needs to be before anything else on page to use $_SESSION
$textarea1 = "";
if(isset($_POST['textarea1'])){
$_SESSION['textarea1'] = $_POST['textarea1'];
}
?>
<form action="page1.php" method="post">
<textarea name="textarea1" id="textarea1"></textarea><br />
<input type="submit" value="submit" />
</form>
<br /><br />
Go to page2
page2.php
<?php
session_start(); // needs to be before anything else on page to use $_SESSION
// this page outputs the textarea1 from the session IF it exists
$textarea1 = ""; // set var to avoid errors
if(isset($_SESSION['textarea1'])){
$textarea1 = $_SESSION['textarea1']
}
?>
<textarea><?php echo $textarea1;?></textarea>
WARNING!!! - This contains no validation!!!

Session variable doesnt change

I want to pass one session variable to other page. On my first page I have:
<?php
session_start();
?>
then:
<form class="form1" method="post" action="contact2.php" id="form1">
<ul>
<li>
<label for="name">*Name:</label>
<input type="text" name="name" placeholder="Black Nova"class="required" role="input" aria-required="true"/>
</li>
<li>
<input id="submit" class="submit .transparentButton" value="Next" type="submit" name="submit"/>
</li>
</ul>
<br/>
</form>
<?php
$_SESSION['name'] = $_POST['name'];
echo $_SESSION['name'];
?>
In my contact.php I have session start, but I cannot get sesssion variable.
If, in my first page, I dont give any action, I get the right value in $_SESSION['name'] but if I give an action, the session variable wont change. Why?
If you do not give action, then the form is being submitted into the same page and so $_POST has value because of which you can get it assigned to $_SESSION. When you give action as contact2.php the form is submitted into a different page and so $_POST will not be available in the page that has the form and so the session will not get any value from it.
If you have set action to contact2.php, you can do a session_start() in that page and move the code
<?php
$_SESSION['name'] = $_POST['name'];
echo $_SESSION['name'];
?>
into that page, and you should be able to echo the session in contact2.php
Its because if you don't supply any action, the form will submit to itself (same page) and because your setting the session variable in the first page it fills the session variable fine. When you add the action to the 2nd page it never fills the session variable because the first page isn't receiving the $_POST, the 2nd page is.
if your submitting form data to a different page, you need to set the session data on the receiving page not the sending page.
When you click submit in your form, form will send you in contact2.php (action="contact2.php").
Create contact2.php and write code below:
session_start();
session_regenerate_id();
$_SESSION['name'] = $_POST['name'];
echo $_SESSION['name'];

Categories