Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

referencing a variable from 1 php file to another

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

referencing a variable from 1 php file to another

Postby sweetpeet » Wed Feb 13, 2013 7:17 pm

Hello all,

I am a newbie to PHP, so I apologize ahead of time if this question seems to be quite elementary.

I have two PHP files, we'll call them file A and file B. The first file, file A grabs all it's values from an html file that uses <form method=post> with name and value, etc.. then I use $_POST to declare each one to a variable. now, I want to use a few of the variables that have already been declared in file A in file B. How exactly do I do that?

Thanks!
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: referencing a variable from 1 php file to another

Postby simplypixie » Wed Feb 13, 2013 11:30 pm

It depends how you want to get them to page B. If page A has another form that is posted to page B then you can just put the variables in has hidden inputs in the form.

If you are not using another form then your only option is to use sessions. Do you need help on using sessions?
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: referencing a variable from 1 php file to another

Postby sweetpeet » Thu Feb 14, 2013 11:50 am

simplypixie wrote:It depends how you want to get them to page B. If page A has another form that is posted to page B then you can just put the variables in has hidden inputs in the form.

If you are not using another form then your only option is to use sessions. Do you need help on using sessions?


yes, I guess than I'll need help using sessions.
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: referencing a variable from 1 php file to another

Postby simplypixie » Thu Feb 14, 2013 12:39 pm

In which case, to give you some guidance and an example.

Firstly ensure that you have session_start(); declared at the very top of the page that you are using sessions on (this means before any other PHP or HTML code):

Code: Select all
<?php
  session_start();
  // Include or Require your external PHP files here as needed
  // Write any other PHP code needed for your page here,
  // including setting the session variables as required
?>
<!-- Your HTML here -->


For your question, in page B, assign the values that have been posted from page A to session variables like so (as you don't say what the names of your form fields are I am having to guess):

Code: Select all
<?php
  session_start();
  $_SESSION['firstname'] = $_POST['firstname'];
  $_SESSION['surname'] = $_POST['surname'];
  $_SESSION['email'] = $_POST['email'];
?>


Then you just echo out the session variables as you would any other variable.
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: referencing a variable from 1 php file to another

Postby seandisanti » Thu Feb 14, 2013 1:55 pm

You can also use cookies, but pointing sessions are still the better way to go. If you want a quick rundown on sessions, check out http://jream.com/learning/videos/php-ba ... e-security It's a 6 minute video that runs you through the essentials with a couple of follow along at your own discretion examples.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: referencing a variable from 1 php file to another

Postby sweetpeet » Thu Feb 14, 2013 4:36 pm

Everything you all have given me has been very helpful.

How do I assign a variable to a session? I have attempted it two ways: $_SESSION['bagelT'] = $bagelT;
$_SESSION['selectBread'] = '$selectBread';. but neither of them return what is defined to the variable.
For the case of bagelT it is as follows:
Code: Select all
$bagelT = '<select name="bagelT"><option value="select">Select One</option><option value="wheat">Wheat Bagel</option><option value="blueberry"> Blueberry Bagel</option><option value="asiago">Asiago Bagel</option><option value="kosher">Kosher Salt and Pepper Bagel</option></select>';

but it does not return the selected value. Am I able to set a session to a variable like that? How do I do this? let me know if you need more information.

edit:
Oh, wait!..... I probably need to add echo. right?


edit:
hmmm...nope
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: referencing a variable from 1 php file to another

Postby simplypixie » Thu Feb 14, 2013 11:30 pm

Please read my post - I have explained it all for you.
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: referencing a variable from 1 php file to another

Postby sweetpeet » Fri Feb 15, 2013 3:44 pm

so, every single variable must look like this: $_SESSION['firstname'] = 'whatever';?
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: referencing a variable from 1 php file to another

Postby seandisanti » Fri Feb 15, 2013 3:52 pm

no, just the session variables.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: referencing a variable from 1 php file to another

Postby sweetpeet » Fri Feb 15, 2013 5:44 pm

seandisanti wrote:no, just the session variables.


ok, but when I enter something like this:

$_session['bagel'] = $bagelT

where,
$bagelT = '<select name="bagelT"><option value="select">Select One</option><option value="wheat">Wheat Bagel</option><option value="blueberry"> Blueberry Bagel</option><option value="asiago">Asiago Bagel</option><option value="kosher">Kosher Salt and Pepper Bagel</option></select>';

all it returns is a dropdown selection when what I really want is the value that it returns.
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: referencing a variable from 1 php file to another

Postby simplypixie » Fri Feb 15, 2013 10:45 pm

I presume you want to get the value selected from the dropdown menu, not just echo out the dropdown menu in future, in which case you don't assign the whole menu to a variable you just have the menu on your form and then get the valu posted from it.

In the form:
Code: Select all
<select name="bagelT"><option value="select">Select One</option><option value="wheat">Wheat Bagel</option><option value="blueberry"> Blueberry Bagel</option><option value="asiago">Asiago Bagel</option><option value="kosher">Kosher Salt and Pepper Bagel</option></select>


In page B where the form dat is submitted to
Code: Select all
$_session['bagel'] = $_POST['bagelT'];


Two things to remember here:

1. When accessing posted data you need to use $_POST['form_element_name']

2. You don't assign form elements to variables in the form
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: referencing a variable from 1 php file to another

Postby sweetpeet » Fri Feb 15, 2013 11:31 pm

simplypixie wrote:I presume you want to get the value selected from the dropdown menu, not just echo out the dropdown menu in future, in which case you don't assign the whole menu to a variable you just have the menu on your form and then get the valu posted from it.

In the form:
Code: Select all
<select name="bagelT"><option value="select">Select One</option><option value="wheat">Wheat Bagel</option><option value="blueberry"> Blueberry Bagel</option><option value="asiago">Asiago Bagel</option><option value="kosher">Kosher Salt and Pepper Bagel</option></select>


In page B where the form dat is submitted to
Code: Select all
$_session['bagel'] = $_POST['bagelT'];


Two things to remember here:

1. When accessing posted data you need to use $_POST['form_element_name']

2. You don't assign form elements to variables in the form




Ok! Thanks for the help
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron