Link to home
Start Free TrialLog in
Avatar of TLN_CANADA
TLN_CANADAFlag for Afghanistan

asked on

Simple Passing PHP Variables Question

Hi Guys,

I want to pass some PHP variables between pages and wondering what the best way to do this is. From my limited understanding of PHP, I'm guessing session variables would be the best way to do this? Once the user in done they will have the option to save all of the variables or to discard it.

Here are the two pages:

http://www.clearthemirror.com/bubble.php

http://www.clearthemirror.com/body.php

Firstly, data is enter on the bubble page and I want this to be accessible on the body page when the user selects the "add body sensations" from the dropdown menu.. When users enter info on the body page I want this to be accessible on the bubble page when they return to it and also to display the information from the first time the user was on the bubble page. (I hope I explained that okay!) :)

Thanks for your help,

Derek
Avatar of Pratima
Pratima
Flag of India image

you want this data from one page to another right ? do not want on any other page then no need to use session in this case

you can use POST method to get data from one page to other using for tag

see the xample on this link

http://stackoverflow.com/questions/6913970/passing-form-data-from-one-web-page-to-another-with-php
here you will get all the ways that pass the variables

http://mrarrowhead.com/index.php?page=php_passing_variables.php
Avatar of TLN_CANADA

ASKER

Hi pratima_mcs,

Yes i will want these variables on two other pages also,

Thanks,

Derek
If I understand your question, you wish that data, once they are inserted in a page, be
accessble from every page (at least body and bubble) during the session: if this is the case,  then you have to use $_SESSION: put there your data and you'll can access and orint them from each page.

Another choice, if you want they are permanently accessible, is to store them in a database, but I don't think this is what you want.

Cheers
then you need use session variables
save post variables in session

see the article Move $_POST Variable Contents into $_SESSION Variable
in above link
Thanks marqusG, yes you are correct, I will only store them in a database later if the users wants them saved. Could you give me an example of how to use them, taking one or two of the variables on the bubble page and passing them to the body page and then printing the session variables?

Many thanks,

Derek
You don't need to pass them. Let's say you have a form in the bubble page:

<form action='bubble.php' method='post'>
<input type='text' name='var1' />
<input type='text' name='var2' />
<input type='submit' name='submit' value='Submit' />
</form>

At the top of bubble.php put this

<?php
if (isset($_POST['submit'])){
    //here the code to validate variables then
    $_SESSION['var1'] = $_POST['var1'];
    $_SESSION['var2'] = $_POST['var2'];
}
?>

Now, in the page body.php (and in any other page) you'll can use them

<?php
if (isset($_SESSION['var1']) && isset($_SESSION['var2']) {
    // use variables as you wish
}
?>

Hope this is clear enough.

Cheers
You need to first start a session in your page

<?php
session_start();

// Now you can start playing with the session variables.
// Given your data is comming back in an array keep it like that so

// You want to ensure that data is available and you might want to do some santizing 
// here as well to prevent hacking attempts.

// Here we get the values for the selects

$_SESSION['select'] = is_array($_POST['select'])?$_POST['select'] : array();
$_SESSION['textfield'] = is_array($_POST['textfield'])?$_POST['textfield'] : array();

// Now your session will hold a value of either an empty array or a filled array

// If you want to access these values on another page (or the same page later) then
// make sure you have the session_start() at the top of each page that will access the session

$select = is_array($_SESSION['select'])?$_SESSION['select'] : array();
$textfield = is_array($_SESSION['textfield'])?$_SESSION['textfield'] : array();

// As before $select[N] and $textfield[N] will hold corresponding values and you can use them accordingly.
?>

Open in new window

ooops! :-)
Here is how to use the PHP session handler.

1. At the very top of your scripts -- all of your scripts -- put this statement, unconditionally, without exception.
session_start();

Open in new window

2. Then place data into the $_SESSION array, and expect to find the data in every script that has followed rule #1.
Rereading the question and looking at the behavior of the two web pages, I think a little more explanation may be in order.

To get variables from the client to the server, you use HTML forms and an HTTP request to an "action" script.  PHP has a tutorial that teaches how that is done.  In the case of the Bubble page, the HTTP request method is POST, so the data arrives in the $_POST array.
http://php.net/manual/en/tutorial.forms.php

To get variables from page to page in a web site, the PHP session can be used, as discussed above.  But there is a potential for problems with the session, especially if your clients may be "on" your site for a long time.  PHP sessions, in effect, time out after 24 minutes of inactivity.  The meaning of "inactivity" is important.  Activity is an HTTP request; inactivity is the absence of an HTTP request.  If your client is filling a long form or doing some other complicated activity, 24 minutes can go by quickly, and when she finally hits the submit button, all of the stateful session data may be gone!  Bummer.

Another way to get variables from page to page is to put a cookie on the client browser and use the cookie as an anchor into your data base.  All of the client variables can be remembered for long periods of time in the data base.  As long as the client browser returns the cookie, you can use the cookie value as a key to locate the client information and return the client to the original state.  This is often used with anonymous shopping carts -- you can shop, stop, and return another day to find your products still in the cart.  Pretty sweet.

Based on your intent to get variables from one page to the next and back (and maybe to other pages in the future) I think I would recommend the combination of the data base and the cookie.  It's a little more work to set up at first, but in the long run it is a much more powerful and flexible way to handle your data.

My $0.02, ~Ray
Hi Julian,

I thought I'd start with the example you provided and try to add session variables from the body.php page and then retrieve and echo them on the bubble.php page.

When I try to echo them though it only prints "array, array" ?

I have attached the files for both pages

Thanks,

Derek
body.php
Hi Julian,

I thought I'd start with the example you provided and try to add session variables from the body.php page and then retrieve and echo them on the bubble.php page.

When I try to echo them though it only prints "array, array" ?

I have attached the files for both pages

Thanks,

Derek
body.php
bubble.php
When you call echo on an array, you get the word "array."  If you want to see what is in the array, do it this way:
var_dump($select);
var_dump($textfield);
var_dump($_SESSION);

Open in new window

to output vars in my code I have the following function

<pre>
<?php
fnDump($_SESSION);

function fnDump($obj)
{
  echo "<pre>";
  print_r($obj);
  echo "</pre>";
}

Open in new window

Thanks Julian,

I put your retrieval code in but it's still coming up blank :

Array
(
    [select_bodypart] => Array
        (
        )

    [sen_description] => Array
        (
        )

)


I've attached the updated page as I'm probably putting it in the wrong place.

Thanks,

Derek
bubble.php
Sorry Julian, I had changed the names in a previous saved version so that is why it wasn't working. I have it displaying the array now correctly. Thank you!

Could I make a change to this that if the array is populated, then the div tag in the centre of the page displays a different image when the page loads  (instead of new_bubble3.jpg) changes to new_bubble3_body.jpg)

Here is the div tag that the change would be applied to:

<div style="background-image:url(Pictures/new_bubble3.jpg);float:left;no-repeat;width:650px;height:500px;text-align:center;padding-top:65px">

Thanks,

D
To determine if array is empty use the empty function
<?php
if (empty($select)) {
  // Show markup for empty array
}
else {
  // Show markup for popuplated array
}
?>

Open in new window


Is that what you were asking?
Yes, if the array is empty then I want it to do nothing. If it is populated I want the page to load with a different image in div.
Yes, if the array is empty then I want it to do nothing
That is a bit confusing - by nothing do you mean load the page but where the image would go don't put anything?

Like this
<!-- Start of page here !>
<?php
if (!empty($select)) : ?>
<img src="someimage.jpg" />
<?php endif;?>
<!--... Rest of page here !>

Open in new window

Sorry. I'll try to be clearer.

If the array is empty I want it to load the page the bubble.php page as normal. (Nothing needs to change on the body.php page)

If the array has a value(s) in it, I would like a different image to appear in the background image of the page. Here is the line in the code that I want to change.

If the array is empty I want it to load the page like this:

<div style="background-image:url(Pictures/new_bubble3.jpg);float:left;no-repeat;width:650px;height:500px;text-align:center;padding-top:65px">

If the array is populated I want it to load the page like this:

<div style="background-image:url(Pictures/new_bubble3_body.jpg);float:left;no-repeat;width:650px;height:500px;text-align:center;padding-top:65px">


I hope I've made it clearer,

Thanks,

D
I just want to make sure I'm printing from this array correctly as I've changed some of the variable names on the other page.

Here are the variables as created on the other page:

<label>Sensation</label>
        <select name="select_bodypart[]">
<label>Description</label>
        <input name="sen_desc[]"

Here is the calling that you suggested:

$_SESSION['select'] = is_array($_POST['select_bodypart'])?$_POST['select_bodypart'] : array();
$_SESSION['textfield'] = is_array($_POST['sen_desc'])?$_POST['sen_desc'] : array();

// Now your session will hold a value of either an empty array or a filled array

// If you want to access these values on another page (or the same page later) then
// make sure you have the session_start() at the top of each page that will access the session

$select = is_array($_SESSION['select'])?$_SESSION['select'] : array();
$textfield = is_array($_SESSION['textfield'])?$_SESSION['textfield'] : array();

And here is what is being outputted from the array when I print it with 2 variables in the array:

Array
(
    [select_bodypart] => Array
        (
        )

    [sen_description] => Array
        (
        )

    [select] => Array
        (
            [0] => 1
            [1] => 3
        )

    [textfield] => Array
        (
            [0] => dfsdfdfdfsdfsf
            [1] => dfsdfdfsdfdfdfsdfdsfdsf
        )

)



Is this correct and is there a reason why the first two variables in the printed array are blank?

Thanks,

Derek
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you! I have the div tag part working correctly now.

I want to be able to store some session variables and pass them onto the other page as well so I've created another question for this if you have a chance to look at it.

https://www.experts-exchange.com/questions/27927641/Storing-Session-Variables-Question.html