How do I retain a variable

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

ronaustin
New php-forum User
New php-forum User
Posts: 11
Joined: Thu Feb 02, 2012 9:04 am

How do I retain a variable

Postby ronaustin » Sat Aug 09, 2014 9:04 am

I wrote 3 programs
Program A is a simple HTML that asks for a User ID using a form. When the form is submitted, Program B starts.

Program B is a PHP program with another HTML form that asks for more information. The user ID field in that form is populated using POST from program A. When this form is submitted, Program C starts.

Program C updates a SQL database and then goes back to Program B. When it does this, the user ID field does not populate. How do I pass a variable from program A to program B to program C and then back to program B?

landi
php-forum Active User
php-forum Active User
Posts: 303
Joined: Thu Mar 15, 2012 3:59 pm

Re: How do I retain a variable

Postby landi » Sat Aug 09, 2014 9:49 am

Hi,
Checkout session handling. Pass the session id between pages (as a GET or POST variable or as a cookie) and use that to lookup other context data (usually from a database).
-A

ronaustin
New php-forum User
New php-forum User
Posts: 11
Joined: Thu Feb 02, 2012 9:04 am

Re: How do I retain a variable

Postby ronaustin » Wed Aug 13, 2014 3:32 pm

I am trying to pass a variable from one php script to another one via SESSION using the 3 programs below. Would someone please tell me why this does
not work.
--------------------------------------------------------------------------------

<html>
<title>session0.html</title>
<body OnLoad="document.logon.logon_id.focus();">
<form name="logon" action="session0.php" method="post">
<table border="1" cellspacing="5%" style="font-size: 125%">
<tr>
<td><p>Logon:&nbsp;&nbsp;</p></td>
<td><input type="text" name="logon_id" /></td>
</tr>

<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
---------------------------------------------------------------------------------------
* this works and shows correct output*

<?php
//session.php
session_start();
$_SESSION['logon'] = $_POST[logon_id];
$user=$_SESSION['logon'];
echo "Logged on as: ". $user;
?>

<html>
<br>
<br>
<a href="http://www.patriotchat.net/progs/session1.php">Click to continue</a>
</html>
----------------------------------------------------------------------------------------------
* This does not work and gives a blank output *

<?php
//session1.php
echo "hello ";

$user=$_SESSION['logon'];
echo "Logged on as: ". $user;
?>

landi
php-forum Active User
php-forum Active User
Posts: 303
Joined: Thu Mar 15, 2012 3:59 pm

Re: How do I retain a variable

Postby landi » Thu Aug 14, 2014 1:12 pm

Hi,
I assume the file marked //session.php is in fact session0.php.

In these lines:

Code: Select all

session_start();
$_SESSION['logon'] = $_POST[logon_id];

what is the value of $_POST[logon_id]? e.g. what does this output:

Code: Select all

echo $_POST[logon_id];

If it is blank, then your answer lies in the syntax of $_POST[...]

-A

ronaustin
New php-forum User
New php-forum User
Posts: 11
Joined: Thu Feb 02, 2012 9:04 am

Re: How do I retain a variable

Postby ronaustin » Thu Aug 14, 2014 4:20 pm

Thank you for replying.

The first program (session0.html) is ran only once and it uses a form to ask for the logon.

The second program (session.php) is also ran only once and it starts the session and is supposed to make $SESSION['LOGON'] a session variable containing the logon id entered in the first program. The second program seems to work. If I enter "bob" as the logon in the first program then the output of the second program shows:

Logged on as: bob

I then link to the third program (session1.php). I start with
echo "Hello";
and then use the exact same code that I used in program 2,

$user=$_SESSION['LOGON'];
echo logged on as: ".$user;

However, the output from this program is "Hello Logged on as: ".

I removed most of the third program. It has another form and $user was supposed to be used to populate one of the fields. From this program the session went to program 4 which updated the data base and then went back to program 3 to fill in the form again, etc.

It just seems unnecessary to ask for the logon each time the form is filled out in program 3. In my first attempt I wrote the logon_id into a text file in the 2nd program and read it back out in the 3rd program. That worked great until I realized that if more than one person used the program the logon file would be overwritten, so I started looking at SESSION and here I am.

I copied and pasted the

$user=$_SESSION['LOGON'];
echo logged on as: ".$user;

from the second program to the third program so I don't understand why it should not work. Obviously I am missing something important here.

landi
php-forum Active User
php-forum Active User
Posts: 303
Joined: Thu Mar 15, 2012 3:59 pm

Re: How do I retain a variable

Postby landi » Thu Aug 14, 2014 11:08 pm

Hi,
The code you posted couldn't work, so if it printed 'Bob' then it wasn't the code you posted :)

Why not post your code?

Obviously I am missing something important here.

Exact code is important. :)

-A

ronaustin
New php-forum User
New php-forum User
Posts: 11
Joined: Thu Feb 02, 2012 9:04 am

Re: How do I retain a variable

Postby ronaustin » Thu Aug 14, 2014 11:24 pm

The only error I saw was that the second program had a remark //session.php when it should have been //session0.php. I added that to program when I posted it to the forum and of course that would not have any effect on the outcome as long as the programs were ran in the correct order.

It is 3:22 am here but tomorrow I will make another post and attach the programs to it.

ronaustin
New php-forum User
New php-forum User
Posts: 11
Joined: Thu Feb 02, 2012 9:04 am

Re: How do I retain a variable

Postby ronaustin » Fri Aug 15, 2014 7:34 am

This forum will not let me attach the files because it thinks it is a security threat.

Here are the three programs. I copied the scripts and pasted them into this one file to make it easier for me to post them.
When I upload these to my server and run them I get the results I explained above
-----------------------------------------------------------------------------------------
<html>
<title>session.html</title>
<body OnLoad="document.logon.logon_id.focus();">
<form name="logon" action="session0.php" method="post">
<table border="1" cellspacing="5%" style="font-size: 125%">
<tr>
<td><p>Logon:&nbsp;&nbsp;</p></td>
<td><input type="text" name="logon_id" /></td>
</tr>

<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
---------------------------------------------------------------------------------------------

<?php
//session0.php
session_start();
$_SESSION['logon'] = $_POST[logon_id];
$user=$_SESSION['logon'];
echo "Logged on as: ". $user;
?>

<html>
<br>
<br>
<a href="session1.php">Click to continue</a>
</html>

------------------------------------------------------------------------------------------------

<?php
//session1.php
echo "hello ";
$user=$_SESSION['logon'];
echo "Logged on as: ". $user;
?>

landi
php-forum Active User
php-forum Active User
Posts: 303
Joined: Thu Mar 15, 2012 3:59 pm

Re: How do I retain a variable

Postby landi » Fri Aug 15, 2014 9:27 am

Hi,
In session0.php, this line needs quotes:
$_SESSION['logon'] = $_POST[logon_id];
e.g.

Code: Select all

$_SESSION['logon'] = $_POST['logon_id'];


I don't use internal session handling, but I suspect you need to call session_start() in session1.php BEFORE you try and retrieve the value of $_SESSION['logon'].

e.g.

Code: Select all

echo "hello ";
session_start();
$user=$_SESSION['logon'];


Hope that helps

-A


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron