passing varables' value from one form to another in php - php

I have three forms - payment.php , payment1.php and paydb.php . payment.php contains the front end form.payment1.php contains the back end of the form of payment.php. whereas we are shifting to paydb.php from payment1.php. Now I'm filling the form by entering member number in payment.php which is retrieved in a variable $member_no in payment1.php .Now I want to get the value of member_no in paydb.php . How to do that ?

After receiving $member_no in payment1.php redirect to paybd.php with a get array
using
header('Location: http://www.example.com/paydb.php?member_no=$member_no');
then receive $_GET['member_no'] number and assign to a variable
example:
$member_no = $_GET['member_no']

The first thing is make sure you are not passing sensitive information where the public can see it. Such as in a URL.
As soon as you get the member's number... store it in a session variable.
You can probably do this when they log in.
session_start();
$_SESSION['member_no'] = $member_no;
OR on the first payment page (assuming 'member_no' is the name of the form element being passed) like this...
$_SESSION['member_no'] = $_POST['member_no'];
Now that session will persist as long as the visitor has their browser open and you don't have to worry about passing it from page to page.
You can use that session on any subsequent page simply by calling it.
<?php echo $_SESSION['member_no'] ?>
Without showing this information to the public.
ALWAYS make sure you place this at the top of any page where you want to use session variables.
if (!isset($_SESSION)) {
session_start();
}

Related

PHP SESSION cant store variable from GET form variable

<?
session_start();
$_SESSION['name'] = "$_GET["name"]";
$_SESSION['email'] = "$_GET["email"]";
$session_id=session_id();
echo"$session_id <br> $_SESSION['name'] <br> $_SESSION['email']";
?>
I am trying to create a session to store visitor input form with GET method, I cant use POST because the form is handled by wordpress plugins and the client only gave me the GET option. The problem is:
On page #1, this is the page after we submit the form, the echo is shown complete.
At page #2, I already add session_start(); at top but $_SESSION['name'] and $_SESSION['email'] keep empty (change page) but $session_id is stored and show same.
What am I missing? or maybe $_SESSION is cant store $_GET?
So on the first page you save the values from the user input and it works as expected.
However, when you go to the second page, your code is overriding the session with new variables, which are empty in this case. Before assigning anything to the session, you should check whether it is not empty / valid.
For this simple code I would recommend checking via isset / array_key_exists functions.

pass session values in pagination

I can pass values form one page to another but I need to pass value like this,
Page 1:
Page2.php
Page3.php
I need to pass the radio button values in the Page1.php to Page2.php, and also i need to use same session . if the form is redirected to page3, I am unable to get the value of page 1. its online quiz project. 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!
The best practice would be to go with OOP. So, you can do the following:
Create a class with fields to hold all the information you want to pass.
class PageInfo
{
var $pageTitle;
var $currentPage;
var $btnValue;
.....
}
2.Now crate an object of PageInfo class, and assign the values you want to set.
$page = new PageInfo();
$page->pageTitle = "Home";
$page->btnValue = 1;
...
3.Assign this object (holding all the details of your page) to the super-global session variable.
// store session data
$_SESSION['page'] = $page;
4.Now you can access the value stored in the session at the different pages.
$otherPage = $_SESSION['page'];
echo $otherPage->pageTitle;
echo $otherPage->btnValue;
Note: The session_start() function must appear BEFORE you print anyting on the page.
Actually your question is vague, first of all when you are using sessions, you need to be sure you are calling session_start() at the very top of the page, secondly, you can save the form data in your session variable like
if(isset($_POST['YOUR_SUBMIT_BUTTON_NAME_HERE'])) {
$store_temp_val1 = $_POST['whatever']; //Sanitize the value first
$_SESSION['page_one']['first_val'] = $store_temp_val1;
}
Now you can simply retrieve the value stored in the session variable on the second page like
echo $_SESSION['page_one']['first_val']; //Will echo the previous page value
Note: Use session_start() on each page at the very top.

how to assign post globals on a redirected page?

I have a login form which sends 3 post values from username, password and submit button. But my form processor has 3 pages one is validation.php which validates the field second is read.php which checks the posted values against db and third is login.php which is a result of login success. All redirect to each other respectively on success. Problem here is that when I try to access the user posted values from form in read.php (redirected page) not validate.php (action page) I get an error of undefined index.
I really don't see why you are doing all those redirects, but if you want to make the data more persistent you could use a session variable, because the $_POST superglobal is only set for the current request.
firstfile.php
<?php
session_start();
$_SESSION['posted_data'] = $_POST;
other file
<?php
session_start();
var_dump($_SESSION['posted_data']);
However as already stated you may really want to reconsider doing all the requests.
UPDATE
Besides the fact that you will loose your data you are also doing multiple (unneeded) requests to simply sumbit the form. The only redirect that should happen is to the successpage when you have done all you work. See this for more information: http://en.wikipedia.org/wiki/Post/Redirect/Get
If you are look to keep you code clean you could always just include the other files or go for an OOP approach.
You should do one page only that will do all the work. That doesn't seem too complicated of a script, so I would advise putting everthing together on one page.
You did not provide any code so I'll show you a general example. I just typed it without rereading so it's not pure PHP syntax, just the spirit:
<?php
$login=$_POST['login'];
$pwd=$_POST['pwd'];
$dbcheck = mysql_fetch_array(mysql_query("SELECT COUNT(1) FROM table WHERE user =$login and pwd = $pwd"))
if($dbcheck[0] > 0) {
//Login success
//Setup your session variables, cookies, etc
//Then you can do your redirect here
} else {
//Page for wrong login
}

Session value doesnt pass on to the next page

Basically I want to grab an id send via the url (ex. www.website.com/?id=432432) and take it accross my website till the user hits the contact page. I created a variable and a session variable
session_start();
$getId = $_GET["id"];
$_SESSION['session_browser_test'] = $getId;
$adv_id = $_SESSION['session_browser_test'];
and used
echo $adv_id;
on my index.php Joomla template so it applies to all the pages.
But the issue is when i go to www.website.com/?id=432432 it echos the id on my web page, but if I click on the next link to go to another page (ex. www.website.com/nextPage) it doesnt hold the session value from the previous page. Why is that? and how can I carry the ID through out the site?
you will not get an id from URL on next page, likely
echo $getId;
instead you need to use id from session like,
$_SESSION['session_browser_test']; // your id stored in session
Start the session in each page
session_start();
In order to access the variable in a session, you have to call the $_SESSION variable.
echo $_SESSION['session_browser_test'];
HTTP is stateless, so you have to do something to remember your variable throughout the website .
make sure you correctly use session , like session_start();
when you send your id through get method ,it works, but when you go to any other page ,it doesn't make any sense to remember this.
use this for send id through pages:
<?php echo get_permalink(910); ?>?userid=<?php echo $value['userId'];?>
send this in url and use on next page as:
$sql = "select * from `wp_pelleresuser` where userId =".$_GET['userid'];
using this approach you can use a single variable on every page you want without using session. try google to how wordpress manage variable through all pages without using session. it will help you.
happy coding!
Start
session_start();
(if not started) in the index.php in root of your app (session probably will start on every pages) and then call (when desired):
$_SESSION['session_browser_test'];
instead of assi8gning this sess var to your own variable and then calling it in different places.
if(isset($_GET["adv_id"])){
$_SESSION['session_browser_test2'] = $_GET["adv_id"];
$adv_id = $_SESSION['session_browser_test2'];
}
else {
$adv_id = $_SESSION['session_browser_test2'];
}

Variable persistence in PHP

i have a php page,
on that page i have text boxes and a submit button,
this button runs php in a section:
if(isset($_POST['Add'])){code}
This works fine here and in that section $name,$base,$location etc are calculated and used. but that section of code generates another submit button that drives another section of code.
it is in this second section of code that i wish to add data to the DB.
Now i already know how to do this, the problem is that the variables $name and so forth have a value of NULL at this point.. but they can only be called after the first code section has been run where they gain value.
How do i maintain these values until the point where i add them?
Resources:
the page feel free to try it out:
location mustbe of the form 'DNN:NN:NN:NN' where D is "D" and N is a 0-9 integer
http://www.teamdelta.byethost12.com/postroute.php
the code of the php file as a text file!
http://www.teamdelta.byethost12.com/postroute.php
lines 116 and 149 are the start of the 2 button run sections!
I think you are looking for PHP's session handling stuff ...
As an example, first page:
session_start(); # start session handling.
$_SESSION['test']='hello world';
exit();
second page:
session_start(); # start session handling again.
echo $_SESSION['test']; # prints out 'hello world'
Behind the scenes, php has set a cookie in the users browser when you first call session start, serialized the $_SESSION array to disk at the end of execution, and then when it receives the cookie back on the next page request, it matches the serialised data and loads it back as the $_SESSION array when you call session_start();
Full details on the session handling stuff:
http://uk.php.net/manual/en/book.session.php
Around the add-button you are creating a second form. If you want to have the data within this form then you will have to create hidden input fields. Because you are sending a second form here. Or you are moving the add button up to the other form.
Or as others are mentioning.. Save the values into a session.
you could store them in a session
// first part of form, store name in session
$_SESSION['name'] = $_POST['name'];
// 2nd part of form, store in database
$name = mysql_real_escape_string($_SESSION['name']);
$sql = "INSERT INTO table (name_column) VALUES ('$name');
You can also try using hidden forms variables to store the data

Categories