referencing a variable from 1 php file to another
Moderators: egami, macek, gesf
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!
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!
- simplypixie
- php-forum Active User
- Posts: 300
- Joined: Sun Dec 11, 2011 12:51 am
- Location: Shrewsbury, Shropshire
- Contact:
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?
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.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?
- simplypixie
- php-forum Active User
- Posts: 300
- Joined: Sun Dec 11, 2011 12:51 am
- Location: Shrewsbury, Shropshire
- Contact:
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):
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):
Then you just echo out the session variables as you would any other variable.
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 -->
Code: Select all
<?php
session_start();
$_SESSION['firstname'] = $_POST['firstname'];
$_SESSION['surname'] = $_POST['surname'];
$_SESSION['email'] = $_POST['email'];
?>
-
- php-forum Fan User
- Posts: 973
- Joined: Mon Oct 01, 2012 12:32 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.
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:
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
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>';
edit:
Oh, wait!..... I probably need to add echo. right?
edit:
hmmm...nope
- simplypixie
- php-forum Active User
- Posts: 300
- Joined: Sun Dec 11, 2011 12:51 am
- Location: Shrewsbury, Shropshire
- Contact:
Please read my post - I have explained it all for you.
-
- php-forum Fan User
- Posts: 973
- Joined: Mon Oct 01, 2012 12:32 pm
no, just the session variables.
ok, but when I enter something like this:seandisanti wrote:no, just the session variables.
$_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.
- simplypixie
- php-forum Active User
- Posts: 300
- Joined: Sun Dec 11, 2011 12:51 am
- Location: Shrewsbury, Shropshire
- Contact:
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:
In page B where the form dat is submitted to
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
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>
Code: Select all
$_session['bagel'] = $_POST['bagelT'];
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
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:In page B where the form dat is submitted toCode: 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>
Two things to remember here:Code: Select all
$_session['bagel'] = $_POST['bagelT'];
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