Session variable can't be access to different page - php

I have a problem on getting the session variables to work on different pages
I have three php scripts
1. index.php (includes a html form)
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['billTo_email'] = $_POST['billTo_email'];
}
?>
<form id = 'form_submit' method="post" action = "NetworkOnline.php">
<td><input type="text" name="billTo_email" id="f_name2" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>
?>
NetworkOnline.php
<?php
session_start();
$_SESSION['billTo_email'] = $_POST["billTo_email"];
?>
$f_name=$_POST["f_name"];
$l_name=$_POST["l_name"];
$billTo_country=$_POST["billTo_country"];
$billTo_city=$_POST["billTo_city"];
$billTo_state=$_POST["billTo_state"];
$billTo_street1=$_POST["billTo_street1"];
$billTo_pin=$_POST["billTo_pin"];
$billTo_email = $_SESSION["billTo_email"];
response.php
<?php
session_start();
$_SESSION["billTo_email"] = $_POST["billTo_email"];
?>
<form name = "form1" method = "post" action="mail.php" >
<input type="hidden" name="order" id="order" value="<?php echo $order ?>" >
<input type="hidden" name="bank" id="bank" value="<?php echo $bank ?>" >
<input type="hidden" name="amount" id="amount" value="<?php echo $amount ?>" >
<input type = "test" name= "email" id ="email" value= "<?php echo $billTo_email ?>">
<input type="submit" id="submit" value="submit" name="submit">
</form>

Try this,
index.php:
<html>
<form id = 'form_submit' method="post" action = "NetworkOnline.php">
<td><input type="text" name="billTo_email" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>
</html>
NetworkOnline.php:
<?php
session_start();
$_SESSION['billTo_email'] = $_POST["billTo_email"];
header('Location: response.php');
?>
response.php:
<?php
session_start();
$billTo_email=$_SESSION["billTo_email"]
?>
<form name = "form1" method = "post" action="mail.php" >
<br />
<br />
<input type = "test" name= "email" value= "<?php echo $billTo_email ?>">
<input type="submit" id="submit" value="submit" name="submit">
</form>

Try below,
Your closing tags are wrong in place,
index.php
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['billTo_email'] = $_POST['billTo_email'];
}
?>
<form id = 'form_submit' method="post" action = "NetworkOnline.php">
<td><input type="text" name="billTo_email" id="f_name2" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>
NetworkOnline.php
<?php
session_start();
$_SESSION['billTo_email'] = $_POST["billTo_email"];
$f_name=$_POST["f_name"];
$l_name=$_POST["l_name"];
$billTo_country=$_POST["billTo_country"];
$billTo_city=$_POST["billTo_city"];
$billTo_state=$_POST["billTo_state"];
$billTo_street1=$_POST["billTo_street1"];
$billTo_pin=$_POST["billTo_pin"];
$billTo_email = $_SESSION["billTo_email"];
?>
response.php
<?php
session_start();
$_SESSION["billTo_email"] = $_POST["billTo_email"];
?>
<form name = "form1" method = "post" action="mail.php" >
<input type="hidden" name="order" id="order" value="<?php echo $order; ?>" >
<input type="hidden" name="bank" id="bank" value="<?php echo $bank; ?>" >
<input type="hidden" name="amount" id="amount" value="<?php echo $amount; ?>" >
<input type = "test" name= "email" id ="email" value= "<?php echo $billTo_email; ?>">
<input type="submit" id="submit" value="submit" name="submit">
</form>

UPDATE:
your index.php
<form id = 'form_submit' method="post" action = "NetworkOnline.php">
<td><input type="text" name="billTo_email" id="f_name2" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit"/></td>
</form>
NOTE: NO need to check if (isset($_POST['submit'])) { ... } here, because index.php send data to NetworkOnline.php.
Your NetworkOnline.php:
<?php
ob_start(); // turn on output buffering
session_start();
if( isset( $_POST['billTo_email'] ) ) { // work only if **billTo_email** has some value
$_SESSION['billTo_email'] = $_POST["billTo_email"];
}
// ?> <-- remove this closing tag
$f_name=$_POST["f_name"]; // this line of code shows undefined variable,
//because in your index.php there is no any f_name field presnet.
// your remaining codes
?>

Related

I would use $ _POST twice in one page

I have a site where I make a payment, a bill is created through it, but the problem is that I can not use $ _POST twice in one, and this example:
<? if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<? } if (isset($_POST['pay'])) {
// MY QUERY HERE
// HEADERS HERE
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<? } ?>
Try this.
Kindly check the code for comment.
<?
if (isset($_POST['submit'])) {
$info = $_POST['info'];
// use echo to display second form
echo '
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<!-- // Note the action value, it's for post back. This allow you to post back to the current page -->
<!-- This is to keep the record passed from the first form -->
<input name="info" value="'. $info .'" type="hidden" />
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>';
} else if (isset($_POST['pay'])) {
// MY QUERY HERE
} else {
// Note the action value, it's for post back. This allow you to post back to the current page
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
}
?>
Not the prettiest way to do this but to achieve your result try this...
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<?php } elseif (isset($_POST['pay'])) {
// Perform query here
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<?php } ?>

can I use $_POST 2 times with different page?

can we use $_POST 2 times with different page ?
this example ..
action.php
<form action="next.php" method="post">
<input name="test" value="test" />
<button type="submit" name="test">submit</button>
</form>
next.php
<?php
$test=$_POST['test'];
?>
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="test1">submit</button>
</form>
next1.php
<?php
echo $test2=$_POST['test1']; >> in here i didnt get test1 value . why ?
?>
Within next.php and action.php you need to change your input type like as
<input type="text" name="test" value="test" />
<input type="text" name="test1" value="<?php echo $test; ?>" />
Within next.php and action.php you were missing the type attr of input tag
and you have same name attributes for submit and input within next.php need to remove or change the value from respective
<input type="submit" value="submit"/>
Below code i have tried my local server and it executed successfully.
action.php:-
<form action="next.php" method="POST">
<input type="text" name="test"/>
<input type="submit" value="submit"/>
</form>
next.php:-
<?php
$test = $_POST['test'];
echo $test;
?>
<form action="next1.php" method="POST">
<input name="test1" value="<?php echo $test; ?>" />
<input type="submit" value="submit"/>
</form>
next1.php:-
<?php
$test2=$_POST['test1'];
echo "------".$test2;
?>
Hope this will help.I think this is achieved your requirement.
If you want to do it within one file e.g. index.php then here is the code
<?php
if(isset($_POST['test'])){
if(isset($_POST['test_text'])){
echo 'Output from NEXT: '.$_POST['test_text'];
}
}
elseif(isset($_POST['test1'])){
if(isset($_POST['test_text1'])){
echo 'Output from NEXT1: '.$_POST['test_text1'];
}
}
?>
<table style="width:100%;">
<tr >
<td style="width:50%;">
<form action="" method="post" name="next">
<input type="text" name="test_text" value="<?php echo isset($_POST['test_text']) ? $_POST['test_text']:''; ?>" />
<button type="submit" name="test">submit test1</button>
</form>
</td>
<td style="width:50%;">
<form action="" method="post" name="next1">
<input type="text1" name="test_text1" value="<?php echo isset($_POST['test_text1']) ? $_POST['test_text1']:''; ?>" />
<button type="submit" name="test1">submit test1</button>
</form>
</td>
</tr>
</table>
I hope this will help.
You need to send the POST value with the second form again, e.g. in a hidden field, otherwise the value won't be sent.
Furthermore your submit button shouldn't have the same name as the input field you want the content from. So change the name of your submit button, e.g.:
action.php
<form action="next.php" method="post"> <input name="test" value="test" /> <button type="submit" name="submit">submit</button> </form>
next.php
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="submit">submit</button>
</form>

Passing form inputs from different pages to submit at final page

I have 4 different pages both with one form each.
I want to gather all the entries on each of the pages and submit once.
Here is code.
Page 1
<form action="page2" method="POST">
<input type="text" name="sex">
<input type="submit" value="Submit">
</form>
Page 2
<form action="page3" method="POST">
<input type="text" name="size">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 3
<form action="page4" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="verNote.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" > <input type="submit" value="Submit">
</form>
Then i will like to get all the infos on verNote.php
<?php
echo $_POST['sex'];
echo '<br>';
echo $_POST['size'];
echo '<br>';
echo $_POST['color'];
echo '<br>';
echo $_POST['likes'];
?>
This code above dont seem to post entries from both pages 1 and 2, just for 3 and 4 alone gets submitted.
Will appreciate immediate assistance form anyone who understands my question.
Regards!
You need to load the hidden fields again each time
Page 3
<form action="B.php" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="B.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
I didn't understand 100% what you're trying to achieve, but have you tried using sessions?
Do this in B.php:
<?php
session_start();
if( isset($_POST['sex']))
$_SESSION['sex'] = $_POST['sex'];
if( isset($_POST['size']))
$_SESSION['size'] = $_POST['size'];
if( isset($_POST['color']))
$_SESSION['color'] = $_POST['color'];
if( isset($_POST['likes']))
$_SESSION['likes'] = $_POST['likes'];
?>
Then you can retrieve the values from any other file, just call session_start(); and use the $_SESSION superglobal.
EDIT
Using sessions, you verNote.php file could be something like this:
<?php
session_start();
echo $_SESSION['sex'];
echo '<br />';
echo $_SESSION['size'];
echo '<br />';
echo $_SESSION['color'];
echo '<br />';
echo $_SESSION['likes'];
echo '<br />';
?>

Cannot get POST variables

I have a PHP file where I am posting variables to the same page but it is not working.
Here is the snippet of that page.
<?php
session_start();
require_once 'connect.php';
require_once 'protect.php';
//$_SESSION['uid'] = 1;
if(isset($_POST['shout']))
echo 'Posted';
if(isset($_POST['submit']))
echo 'Posted';
?>
<form method="POST" action="">
<input type="text" name="shout">
<input type="text" name="name">
<input type="submit" value="Shout!">
</form>
<input name="submit" type="submit" value="Shout!">
Name attribute is missing
<?php session_start();
require_once 'connect.php';
require_once 'protect.php';
//$_SESSION['uid'] = 1;
if(isset($_POST['button_name']))
echo 'Posted';
?>
<form name="form_name" method="POST" action="">
<input type="text" name="shout">
<input type="text" name="name">
<input name="button_name" type="submit" value="Shout!">
</form>
Not tested...Hope this helps...
Comment out the required files. If it shows "Posted", then PHP couldn't find the required files, or $_POST was changed within these files.
<?php session_start();
//require_once 'connect.php';
//require_once 'protect.php';
//$_SESSION['uid'] = 1;
if(isset($_POST['shout']))
echo 'Posted';
if(isset($_POST['submit']))
echo 'Posted';
?>
<form method="POST" action="">
<input type="text" name="shout">
<input type="text" name="name">
<input type="submit" name="submit" value="Shout!">
</form>

value preserving across multiple pages

i am trying to post the some data on another page at this i have a back button onclick of back button i wanna reload the original page with posted data.
use $_SESSION:
Page1.php
<?php
session_start();
$name = isset($_SESSION['name']) ? $_SESSION['name'] : '';
?>
<html>
<form method="post" action="Page2.php">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="submit" value="Submit" />
</form>
</html>
Page2.php
<?php
session_start();
if (isset($_POST['name']))
{
$_SESSION['name'] = $_POST['name'];
}
$name = $_SESSION['name'];
?>
<html>
<form>
<span><?php echo $name; ?>" </span>
Back
</form>
</html>
you can use $_SESSION
<?php
if (!isset($_SESSION)) session_start();
if ($_POST['btnsubmit']){
$_SESSION['data1'] = $_POST['data1'];
$_SESSION['data2'] = $_POST['data2'];
$_SESSION['data3'] = $_POST['data3'];
//... and so on
}
?>
<form>
<input type="text" id="data1" value="<? echo $_SESSION['data1'];?>"/>
<input type="text" id="data2" value="<? echo $_SESSION['data2'];?>"/>
<input type="text" id="data3" value="<? echo $_SESSION['data3'];?>"/>
<input type="submit" id="btnsubmit" value="Submit"/>
</form>
<!-- and so on -->
Hope this gives you an idea

Categories