You are here: Re: Newbie question(s) #2: Creating 'output' with form-collected variables « PHP Programming Language « IT news, forums, messages
Re: Newbie question(s) #2: Creating 'output' with form-collected variables

Posted by Kimmo Laine on 02/19/07 17:53

murrayatuptowngallery@yahoo.com kirjoitti:
> Started a new post because this has nothing to do with my last post on
> view source etc.
>
> Some dumb questions may follow...
>
> I am learning PHP for a simple specialized calculator/curve-fitter
> that collects input from a user then fits the data to their range of
> conditions in the form of a table (actually just two columns of
> output).
>
> I bought a copy of Dmitri Koterov's book with a title something like
> "Create your Web Page with PHP'. What I like about it is it explains
> the how and why significantly instead of just examples.
> I may eventually find an answer in there, but I'd like to supplement
> reading with questions...
>
> It APPEARS to me that my task will be even easier with PHP than
> previous efforts with JavaScript.
>
> It looks like I can simply ECHO the processed data embedded in tags,
> like a <LI> list or even a <TABLE> . With JS I got lost trying to
> figure out other examples people referred me to with get inner html &
> item ID etc...got too abstract for me.
>
> It doesn't need to be fancy...thus far, I think the echo method may be
> adequate.
>
> So, at last, the question...I previously had trouble with JS figuring
> out how to make variables accessible to more than one script (make
> them 'global'?).
>
> I think PHP is more flexible with variables...whether or not it is a
> good practice, they apparently do not need to be explicitly defined by
> type (they are interpreted by content?), but are they automatically
> assumed to be global?
>
> What do I need to do to them to make them be 'global' or "remembered"
> for use by more than one script? What then would be their
> 'lifetime'...cleared when the script is exit-ed, or until refreshed by
> a new form Submit? (Maybe what I'm hinting at is whether or not there
> is the concept of a 'session' that gets reset somehow.

If you want php to remember values between subsequent page calls, then
I'd recommend you to place all the variables you wish to keep into the
special $_SESSION array. Your book might have a chapter about using
sessions, and another place to learn about using session data is the
php.net manual.

Here's a really brief example with some commenting to get you started:

We'll call this examplepage1.php:
<?php
// The next line is important, it tells php that you wish
// to store session data in this script
sessions_start();

// Now we have some form-filling data from client
// and we perform a calculation:
if(isset($_GET['submit']))
$_SESSION['stuff'] = $_GET['a'] * $_GET['b'] - $_GET['c'];

// Go to examplepage2.php, do not pass go, do not collect $200.
?>

And this shall be known as examplepage2.php:
<?php
// Again at the start of page cos we want to use session data here
sessions_start();

// This is how php "remembers" the previous value
$stuff = $_SESSION['stuff'];

echo "The result of the previous calculation was: $stuff";
?>

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация