Passing Variables

Passing Variables

am 05.04.2008 21:27:31 von mtek

Hi,

I'm hoping that this can be done.

I have a form, and upon clicking the submit button it calls another
PHP script to save the data in a MySQL database. Of course since it
is a FORM, all the variables are available to the save script. Once
the data has been saved, I change the location and go to a summary
page so the customer can see the verification and such.

The problem is when I go from the save script to the confirmation
script, all the variables are lost. The save script is a reusable
script that several scripts use.

I have a couple of questions. Instead of calling the save script and
then going to another page, if I make it an include function, will
that prevent the loss of variables?

If not, is there another way to do this, since the save script is
nothing more than PHP code, with no forms or data input....

Thanks for everyone's help.

John

Re: Passing Variables

am 05.04.2008 21:52:20 von paulb

Mtek wrote:
> Hi,
>
> I'm hoping that this can be done.
>
> I have a form, and upon clicking the submit button it calls another
> PHP script to save the data in a MySQL database. Of course since it
> is a FORM, all the variables are available to the save script. Once
> the data has been saved, I change the location and go to a summary
> page so the customer can see the verification and such.
>
> The problem is when I go from the save script to the confirmation
> script, all the variables are lost. The save script is a reusable
> script that several scripts use.
>
> I have a couple of questions. Instead of calling the save script and
> then going to another page, if I make it an include function, will
> that prevent the loss of variables?
>
> If not, is there another way to do this, since the save script is
> nothing more than PHP code, with no forms or data input....
>
> Thanks for everyone's help.
>
> John

GET

A few lines of script including an HREF HTML link like this

//Test for empty notes and skip link if so
if ($not == "

")
{
echo " $ln";
}
else
{
?>  "> echo $ln; ?> }


pass the variable mem to another script (below) which picks up the variable
via a GET function and uses it to identify a dataset from a MySQL database
and display the results.


Here's a page I prepared earlier.

include '../lib/opendb.php';

//isset - IS a variable SET hence ISSET
if (isset($_GET['mem'])) { //has a start, end, next or previous image been
clicked?
$mem = $_GET['mem']; //if so get the new page number, if not just continue
with the old
}

//Set membership number
$num = $mem;

//build and execute the query
$query = "SELECT Firstname, Lastname, Notes FROM fr WHERE Number = $num";

//Creates an associative array called $result
$result = mysql_query($query);

//Fetch array from query result
$row= mysql_fetch_array($result);

//Show array structure
//print_r($row)."
";

//Get firstname to var
$fn = $row[0];

//Get lastname to var
$ln = $row[1];


//Write title to screen
echo "

Member notes for $fn $ln


";

//Display result to screen
echo $row[2];

include '../lib/closedb.php';
include '../fil/axs.txt';
?>

See all this in action at http://www.greenhithe.org.uk/dev/fr_table.php and
click any link to see it work, and try altering one of the numbers in the
URL to see what you get, nothing probably.

Paul

Re: Passing Variables

am 05.04.2008 21:56:27 von mtek

On Apr 5, 2:52=A0pm, "PaulB" wrote:
> Mtek wrote:
> > Hi,
>
> > I'm hoping that this can be done.
>
> > I have a form, and upon clicking the submit button it calls another
> > PHP script to save the data in a MySQL database. =A0Of course since it
> > is a FORM, all the variables are available to the save script. =A0Once
> > the data has been saved, I change the location and go to a summary
> > page so the customer can see the verification and such.
>
> > The problem is when I go from the save script to the confirmation
> > script, all the variables are lost. =A0The save script is a reusable
> > script that several scripts use.
>
> > I have a couple of questions. =A0Instead of calling the save script and
> > then going to another page, if I make it an include function, will
> > that prevent the loss of variables?
>
> > If not, is there another way to do this, since the save script is
> > nothing more than PHP code, with no forms or data input....
>
> > Thanks for everyone's help.
>
> > John
>
> GET
>
> A few lines of script including an HREF HTML link like this
>
> //Test for empty notes and skip link if so
> =A0if ($not == "

")
> =A0 {
> =A0 echo " $ln";
> =A0 }
> =A0 else
> =A0 {
> =A0 ?>  ?> "> > echo $ln; ?> > =A0 }
>
> pass the variable mem to another script (below) which picks up the variabl=
e
> via a GET function and uses it to identify a dataset from a MySQL database=

> and display the results.
>
> Here's a page I prepared earlier.
>
> > include '../lib/opendb.php';
>
> //isset - IS a variable SET hence ISSET
> if (isset($_GET['mem'])) { //has a start, end, next or previous image been=

> clicked?
> =A0 $mem =3D $_GET['mem']; //if so get the new page number, if not just co=
ntinue
> with the old
>
> }
>
> //Set membership number
> $num =3D $mem;
>
> //build and execute the query
> $query =3D "SELECT Firstname, Lastname, Notes FROM fr WHERE Number =3D $nu=
m";
>
> //Creates an associative array called $result
> $result =3D mysql_query($query);
>
> //Fetch array from query result
> $row=3D mysql_fetch_array($result);
>
> //Show array structure
> //print_r($row)."
";
>
> //Get firstname to var
> $fn =3D $row[0];
>
> //Get lastname to var
> $ln =3D $row[1];
>
> //Write title to screen
> echo "

Member notes for $fn $ln


";
>
> //Display result to screen
> echo $row[2];
>
> include '../lib/closedb.php';
> include '../fil/axs.txt';
> ?>
>
> See all this in action athttp://www.greenhithe.org.uk/dev/fr_table.phpand
> click any link to see it work, and try altering one of the numbers in the
> URL to see what you get, nothing probably.
>
> Paul- Hide quoted text -
>
> - Show quoted text -

I tried making the save routine an include and just calling that
function passing parameters. That seemed to work fine. But, if I
have a form on the page and need to call that function as my ACTION
item, can I do that?

Or, do I not need a form anymore, since to save I am just calling a
PHP function which is included, and everything on the page is
available to the entire script. So, I can pass them as parameters to
the include function without a form.....

Thanks again!

John

Re: Passing Variables

am 05.04.2008 22:01:10 von paulb

Mtek wrote:
>
> I tried making the save routine an include and just calling that
> function passing parameters. That seemed to work fine. But, if I
> have a form on the page and need to call that function as my ACTION
> item, can I do that?
>
> Or, do I not need a form anymore, since to save I am just calling a
> PHP function which is included, and everything on the page is
> available to the entire script. So, I can pass them as parameters to
> the include function without a form.....

Being a newbie I don't have the knowledge required to answer your precise
question but I do know that a form can use POST or GET as its method so why
don't you use GET as your method and collect the data from the URL?

Sorry I don't have the knowledge to help you beyond this point.

Paul

Re: Passing Variables

am 05.04.2008 22:10:51 von mtek

On Apr 5, 3:01=A0pm, "PaulB" wrote:
> Mtek wrote:
>
> >
>
> > I tried making the save routine an include and just calling that
> > function passing parameters. =A0That seemed to work fine. =A0But, if I
> > have a form on the page and need to call that function as my ACTION
> > item, can I do that?
>
> > Or, do I not need a form anymore, since to save I am just calling a
> > PHP function which is included, and everything on the page is
> > available to the entire script. =A0So, I can pass them as parameters to
> > the include function without a form.....
>
> Being a newbie I don't have the knowledge required to answer your precise
> question but I do know that a form can use POST or GET as its method so wh=
y
> don't you use GET as your method and collect the data from the URL?
>
> Sorry I don't have the knowledge to help you beyond this point.
>
> Paul

We do not want to pass the data via URL. We want to make it
transparent. Just call the function when the HREF is clicked and save
the data in the function.

Re: Passing Variables

am 05.04.2008 22:17:22 von paulb

Mtek wrote:
> On Apr 5, 3:01 pm, "PaulB" wrote:
>> Mtek wrote:
>>
>> >
>>
>>> I tried making the save routine an include and just calling that
>>> function passing parameters. That seemed to work fine. But, if I
>>> have a form on the page and need to call that function as my ACTION
>>> item, can I do that?
>>
>>> Or, do I not need a form anymore, since to save I am just calling a
>>> PHP function which is included, and everything on the page is
>>> available to the entire script. So, I can pass them as parameters to
>>> the include function without a form.....
>>
>> Being a newbie I don't have the knowledge required to answer your
>> precise question but I do know that a form can use POST or GET as
>> its method so why don't you use GET as your method and collect the
>> data from the URL?
>>
>> Sorry I don't have the knowledge to help you beyond this point.
>>
>> Paul
>
> We do not want to pass the data via URL. We want to make it
> transparent. Just call the function when the HREF is clicked and save
> the data in the function.

Ok my last attempt, can you use POST method in the form and pick up the data
using $_POST. I can't comment beyond that, and it won't appear in the URL

Paul

Re: Passing Variables

am 05.04.2008 22:24:40 von Michael Fesser

..oO(Mtek)

>I'm hoping that this can be done.
>
>I have a form, and upon clicking the submit button it calls another
>PHP script to save the data in a MySQL database. Of course since it
>is a FORM, all the variables are available to the save script. Once
>the data has been saved, I change the location and go to a summary
>page so the customer can see the verification and such.
>
>The problem is when I go from the save script to the confirmation
>script, all the variables are lost. The save script is a reusable
>script that several scripts use.
>
>I have a couple of questions. Instead of calling the save script and
>then going to another page, if I make it an include function, will
>that prevent the loss of variables?

No. You have to understand how HTTP works. It's a stateless protocol.
Every request is completely independent from each other. By default
there's no way to carry variables from one request to another, unless
you do it yourself.

Have a look at sessions to store your data and keep it available across
multiple requests.

Micha

Re: Passing Variables

am 05.04.2008 23:23:37 von Jerry Stuckle

Mtek wrote:
> Hi,
>
> I'm hoping that this can be done.
>
> I have a form, and upon clicking the submit button it calls another
> PHP script to save the data in a MySQL database. Of course since it
> is a FORM, all the variables are available to the save script. Once
> the data has been saved, I change the location and go to a summary
> page so the customer can see the verification and such.
>
> The problem is when I go from the save script to the confirmation
> script, all the variables are lost. The save script is a reusable
> script that several scripts use.
>
> I have a couple of questions. Instead of calling the save script and
> then going to another page, if I make it an include function, will
> that prevent the loss of variables?
>
> If not, is there another way to do this, since the save script is
> nothing more than PHP code, with no forms or data input....
>
> Thanks for everyone's help.
>
> John
>

John,

When you "call another script", I suspect you're doing this with the
header() function. If so, this is a new request to the browser - the
current page ends and an entirely new one starts up.

OTOH, if you include the other page, it's code is copied into the
current page, and no new request is made. Therefore all the variables
are available.

There is another (and preferred) way to pass data between pages like
this - take a look at sessions in the doc. They're very easy to use and
solve your problem for you.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Passing Variables

am 05.04.2008 23:27:43 von Jerry Stuckle

Mtek wrote:
> On Apr 5, 2:52 pm, "PaulB" wrote:
>> Mtek wrote:
>>> Hi,
>>> I'm hoping that this can be done.
>>> I have a form, and upon clicking the submit button it calls another
>>> PHP script to save the data in a MySQL database. Of course since it
>>> is a FORM, all the variables are available to the save script. Once
>>> the data has been saved, I change the location and go to a summary
>>> page so the customer can see the verification and such.
>>> The problem is when I go from the save script to the confirmation
>>> script, all the variables are lost. The save script is a reusable
>>> script that several scripts use.
>>> I have a couple of questions. Instead of calling the save script and
>>> then going to another page, if I make it an include function, will
>>> that prevent the loss of variables?
>>> If not, is there another way to do this, since the save script is
>>> nothing more than PHP code, with no forms or data input....
>>> Thanks for everyone's help.
>>> John
>> GET
>>
>> A few lines of script including an HREF HTML link like this
>>
>> //Test for empty notes and skip link if so
>> if ($not == "

")
>> {
>> echo " $ln";
>> }
>> else
>> {
>> ?>  "> >> echo $ln; ?> >> }
>>
>> pass the variable mem to another script (below) which picks up the variable
>> via a GET function and uses it to identify a dataset from a MySQL database
>> and display the results.
>>
>> Here's a page I prepared earlier.
>>
>> >> include '../lib/opendb.php';
>>
>> //isset - IS a variable SET hence ISSET
>> if (isset($_GET['mem'])) { //has a start, end, next or previous image been
>> clicked?
>> $mem = $_GET['mem']; //if so get the new page number, if not just continue
>> with the old
>>
>> }
>>
>> //Set membership number
>> $num = $mem;
>>
>> //build and execute the query
>> $query = "SELECT Firstname, Lastname, Notes FROM fr WHERE Number = $num";
>>
>> //Creates an associative array called $result
>> $result = mysql_query($query);
>>
>> //Fetch array from query result
>> $row= mysql_fetch_array($result);
>>
>> //Show array structure
>> //print_r($row)."
";
>>
>> //Get firstname to var
>> $fn = $row[0];
>>
>> //Get lastname to var
>> $ln = $row[1];
>>
>> //Write title to screen
>> echo "

Member notes for $fn $ln


";
>>
>> //Display result to screen
>> echo $row[2];
>>
>> include '../lib/closedb.php';
>> include '../fil/axs.txt';
>> ?>
>>
>> See all this in action athttp://www.greenhithe.org.uk/dev/fr_table.phpand
>> click any link to see it work, and try altering one of the numbers in the
>> URL to see what you get, nothing probably.
>>
>> Paul- Hide quoted text -
>>
>> - Show quoted text -
>
> I tried making the save routine an include and just calling that
> function passing parameters. That seemed to work fine. But, if I
> have a form on the page and need to call that function as my ACTION
> item, can I do that?
>
> Or, do I not need a form anymore, since to save I am just calling a
> PHP function which is included, and everything on the page is
> available to the entire script. So, I can pass them as parameters to
> the include function without a form.....
>
> Thanks again!
>
> John
>

John,

You can't call a function from a form - you have to access a page. But
code in that page can call a function.

But I don't understand your comment "I do not need a form anymore...".
The purpose of a form is to collect information from the user. Why
would you not need a form?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Passing Variables

am 06.04.2008 03:18:38 von mtek

On Apr 5, 4:27=A0pm, Jerry Stuckle wrote:
> Mtek wrote:
> > On Apr 5, 2:52 pm, "PaulB" wrote:
> >> Mtek wrote:
> >>> Hi,
> >>> I'm hoping that this can be done.
> >>> I have a form, and upon clicking the submit button it calls another
> >>> PHP script to save the data in a MySQL database. =A0Of course since it=

> >>> is a FORM, all the variables are available to the save script. =A0Once=

> >>> the data has been saved, I change the location and go to a summary
> >>> page so the customer can see the verification and such.
> >>> The problem is when I go from the save script to the confirmation
> >>> script, all the variables are lost. =A0The save script is a reusable
> >>> script that several scripts use.
> >>> I have a couple of questions. =A0Instead of calling the save script an=
d
> >>> then going to another page, if I make it an include function, will
> >>> that prevent the loss of variables?
> >>> If not, is there another way to do this, since the save script is
> >>> nothing more than PHP code, with no forms or data input....
> >>> Thanks for everyone's help.
> >>> John
> >> GET
>
> >> A few lines of script including an HREF HTML link like this
>
> >> //Test for empty notes and skip link if so
> >> =A0if ($not == "

")
> >> =A0 {
> >> =A0 echo " $ln";
> >> =A0 }
> >> =A0 else
> >> =A0 {
> >> =A0 ?>  m); ?> "> > >> echo $ln; ?> > >> =A0 }
>
> >> pass the variable mem to another script (below) which picks up the vari=
able
> >> via a GET function and uses it to identify a dataset from a MySQL datab=
ase
> >> and display the results.
>
> >> Here's a page I prepared earlier.
>
> >> > >> include '../lib/opendb.php';
>
> >> //isset - IS a variable SET hence ISSET
> >> if (isset($_GET['mem'])) { //has a start, end, next or previous image b=
een
> >> clicked?
> >> =A0 $mem =3D $_GET['mem']; //if so get the new page number, if not just=
continue
> >> with the old
>
> >> }
>
> >> //Set membership number
> >> $num =3D $mem;
>
> >> //build and execute the query
> >> $query =3D "SELECT Firstname, Lastname, Notes FROM fr WHERE Number =3D =
$num";
>
> >> //Creates an associative array called $result
> >> $result =3D mysql_query($query);
>
> >> //Fetch array from query result
> >> $row=3D mysql_fetch_array($result);
>
> >> //Show array structure
> >> //print_r($row)."
";
>
> >> //Get firstname to var
> >> $fn =3D $row[0];
>
> >> //Get lastname to var
> >> $ln =3D $row[1];
>
> >> //Write title to screen
> >> echo "

Member notes for $fn $ln


";
>
> >> //Display result to screen
> >> echo $row[2];
>
> >> include '../lib/closedb.php';
> >> include '../fil/axs.txt';
> >> ?>
>
> >> See all this in action athttp://www.greenhithe.org.uk/dev/fr_table.phpa=
nd
> >> click any link to see it work, and try altering one of the numbers in t=
he
> >> URL to see what you get, nothing probably.
>
> >> Paul- Hide quoted text -
>
> >> - Show quoted text -
>
> > I tried making the save routine an include and just calling that
> > function passing parameters. =A0That seemed to work fine. =A0But, if I
> > have a form on the page and need to call that function as my ACTION
> > item, can I do that?
>
> > Or, do I not need a form anymore, since to save I am just calling a
> > PHP function which is included, and everything on the page is
> > available to the entire script. =A0So, I can pass them as parameters to
> > the include function without a form.....
>
> > Thanks again!
>
> > John
>
> John,
>
> You can't call a function from a form - you have to access a page. =A0But
> code in that page can call a function.
>
> But I don't understand your comment "I do not need a form anymore...".
> The purpose of a form is to collect information from the user. =A0Why
> would you not need a form?
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================- Hide quoted text -=

>
> - Show quoted text -


Well, say I have page 1 which collects data, and it calls a save
scripts, whch when done calls another page.

Rather, I'll put the save script as an include at the top of the
second page and call that from the first page form. All the variables
will be available since they came from a form, and, I'll have the code
necessary to save and be able to display everything I need to
display.......

In fact, I worked on it a bit and have it working. :)

Re: Passing Variables

am 06.04.2008 04:50:26 von Jerry Stuckle

Mtek wrote:
> On Apr 5, 4:27 pm, Jerry Stuckle wrote:
>> Mtek wrote:
>>> On Apr 5, 2:52 pm, "PaulB" wrote:
>>>> Mtek wrote:
>>>>> Hi,
>>>>> I'm hoping that this can be done.
>>>>> I have a form, and upon clicking the submit button it calls another
>>>>> PHP script to save the data in a MySQL database. Of course since it
>>>>> is a FORM, all the variables are available to the save script. Once
>>>>> the data has been saved, I change the location and go to a summary
>>>>> page so the customer can see the verification and such.
>>>>> The problem is when I go from the save script to the confirmation
>>>>> script, all the variables are lost. The save script is a reusable
>>>>> script that several scripts use.
>>>>> I have a couple of questions. Instead of calling the save script and
>>>>> then going to another page, if I make it an include function, will
>>>>> that prevent the loss of variables?
>>>>> If not, is there another way to do this, since the save script is
>>>>> nothing more than PHP code, with no forms or data input....
>>>>> Thanks for everyone's help.
>>>>> John
>>>> GET
>>>> A few lines of script including an HREF HTML link like this
>>>> //Test for empty notes and skip link if so
>>>> if ($not == "

")
>>>> {
>>>> echo " $ln";
>>>> }
>>>> else
>>>> {
>>>> ?>  "> >>>> echo $ln; ?> >>>> }
>>>> pass the variable mem to another script (below) which picks up the variable
>>>> via a GET function and uses it to identify a dataset from a MySQL database
>>>> and display the results.
>>>> Here's a page I prepared earlier.
>>>> >>>> include '../lib/opendb.php';
>>>> //isset - IS a variable SET hence ISSET
>>>> if (isset($_GET['mem'])) { //has a start, end, next or previous image been
>>>> clicked?
>>>> $mem = $_GET['mem']; //if so get the new page number, if not just continue
>>>> with the old
>>>> }
>>>> //Set membership number
>>>> $num = $mem;
>>>> //build and execute the query
>>>> $query = "SELECT Firstname, Lastname, Notes FROM fr WHERE Number = $num";
>>>> //Creates an associative array called $result
>>>> $result = mysql_query($query);
>>>> //Fetch array from query result
>>>> $row= mysql_fetch_array($result);
>>>> //Show array structure
>>>> //print_r($row)."
";
>>>> //Get firstname to var
>>>> $fn = $row[0];
>>>> //Get lastname to var
>>>> $ln = $row[1];
>>>> //Write title to screen
>>>> echo "

Member notes for $fn $ln


";
>>>> //Display result to screen
>>>> echo $row[2];
>>>> include '../lib/closedb.php';
>>>> include '../fil/axs.txt';
>>>> ?>
>>>> See all this in action athttp://www.greenhithe.org.uk/dev/fr_table.phpand
>>>> click any link to see it work, and try altering one of the numbers in the
>>>> URL to see what you get, nothing probably.
>>>> Paul- Hide quoted text -
>>>> - Show quoted text -
>>> I tried making the save routine an include and just calling that
>>> function passing parameters. That seemed to work fine. But, if I
>>> have a form on the page and need to call that function as my ACTION
>>> item, can I do that?
>>> Or, do I not need a form anymore, since to save I am just calling a
>>> PHP function which is included, and everything on the page is
>>> available to the entire script. So, I can pass them as parameters to
>>> the include function without a form.....
>>> Thanks again!
>>> John
>> John,
>>
>> You can't call a function from a form - you have to access a page. But
>> code in that page can call a function.
>>
>> But I don't understand your comment "I do not need a form anymore...".
>> The purpose of a form is to collect information from the user. Why
>> would you not need a form?
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================- Hide quoted text -
>>
>> - Show quoted text -
>
>
> Well, say I have page 1 which collects data, and it calls a save
> scripts, whch when done calls another page.
>
> Rather, I'll put the save script as an include at the top of the
> second page and call that from the first page form. All the variables
> will be available since they came from a form, and, I'll have the code
> necessary to save and be able to display everything I need to
> display.......
>
> In fact, I worked on it a bit and have it working. :)
>

That's great if you can include the files. A lot of times you can't.

Whatever works for you.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================