Re: New PHP User with a simple question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Jan 25, 2009 at 02:29:50AM -0500, Christopher W wrote:

> Sorry, I am also new to the etiquette of these mail lists.
> 
> Anyway what I was attempting to do, in the full picture, was be able to just
> switch the text in the text area without actually changing pages.  For
> example, if the user clicks "About Us" (from the home page) the page doesn't
> change, just the text (in the area I designated for text).
> 
> Since I have never used php before (but have read some online and in books)
> what I was trying was:
> 
> if ($page == "home") {echo $home_text;}
> elseif ($page == "about") {echo $about_text;}
> ...
> else {echo $error_text;}
> 
> My problem is that I can't figure out how to get the link-click to assign
> the value to the variable.  I didn't try any php for that end because I
> really didn't know where to begin.
> 
> Thanks for the replies and the help.  I truly appreciate it.

The reply I gave you earlier assumes you're doing this with a button,
not a link. If you're doing it with a link, it's slightly different. But
here's what you have to understand first: When you click on a link, you
load a different (or the same) page. Period. Web pages run on the HTTP
protocol, and one of the things about that protocol is that the server
knows virtually nothing about the context in which it's loading a page.
In other words, if you were on Page A and you go to Page B, the HTTP
protocol ensures that the server has *almost* no idea of what you did on
that page. There are some exceptions, two of which are GET and POST
variables. If you did something on a form in the page you came from,
then GET/POST variable will be visible to the server (and PHP) when you
get to the next page. If the "method" on your form is "post", as in:

<form action="index.php" method="post">

then it will see POST variable. If you used the GET method instead, it
will see GET variables.

So if you want to communicate something to the next page you go to, you
will need to do it using a GET variable. GET variables are visible in
the navigation bar above your browser, and POST variables aren't.

So let's assume you want content.php to show "home" stuff if the user
was in index.php and pressed the "home" button. Then for the link the
push, you can do this:

<a href="content.php?section=home">Home</a>

Note the "?section=home" part on the end of the URL? That's a GET
variable named "section" and it contains the contents "home". When you
construct the content.php page. Put in a "variant" section as I
explained in the last email, except make sure it tests for the GET
variable "section", like:

if ($_GET['section'] == 'home') ...

If you've programmed in other languages, PHP is a little difficult to
grasp, just because it has to deal with the HTTP protocol, and you're
embedding PHP in HTML pages. Otherwise its syntax is almost completely
C-like.

HTH,

Paul
-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux