Re: button, parameter and reload - How do I glue it all together? [message #178788 is a reply to message #178785] |
Fri, 03 August 2012 15:50   |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 8/3/2012 10:29 AM, mwoolley wrote:
> I have a webpage, on which the content displayed depends on a parameter passed to or obtained by the webpage. This might be a $_GET variable, a $_SESSION variable or a cookie. I don't mind which is used. (I am open to suggestions as to which is the best method. I think $_SESSION.) When the page is initially displayed, the parameter is unset.
>
> On said page, there is to be a button, which when clicked, will increment the parameter and reload the page. Obviously, the parameter has to have the new value, so the reload causes the page contents to change. This ooperation might be repeated ad-nausem.
>
> Most of the indivual pieces of this (getting the paramter, and having the button reload the page) I can do, but I'm having trouble glueing it all together. I've managed to increment the parameter when the button is pressed, but only by calling an external form tag handler. If I do this, I can't get the page to reload.
>
> By what mechanism, can I have the button increment the parameter and cause the page reload?
>
> I hope this make sense; the old grey matter is addled today!
>
> Thanks in advance.
> Martin
>
OK, let's start from the top. Since you want to display new content,
you must make some type of request to the server (a good thing, since
PHP is server-side :) ).
There are a couple of ways to do this. The easiest is to put your
button in a form and make it a submit button. Then the request will go
to the server for processing. You could also use something like AJAX,
but that seems a bit of overkill for something so simple.
As to the variable you want to increment - you're correct, you can keep
it several places. Where you keep it is pretty much dependent on how
secure you want it. For instance, if it's a page number of a catalog,
the user might want to be able to bookmark this particular page. In that
case I would submit the form with method=GET with the value in the form
(hidden or displayed field - whichever you want). This way when the
user clicks the button the new page number will appear in the URL for
easy bookmarking.
OTOH, if you don't want them to be able to go to that page but have to
always start from the beginning (but otherwise security is not
important), you could use method=POST in your form. This way the page
number wouldn't show up in the url (but is still in a field on the page).
Finally, if security is a concern to you, then you shouldn't even pass
the value in the form (values passed in $_POST can be easily spoofed).
In this case you should use the $_SESSION variable and not even pass the
value to the client. That way the request can't be spoofed. But the
problem here is if the user has two tabs open to your site, the will
share the same value. With GET or POST and the value in the page, each
tab would contain it's own value, so they would be separate. There are
ways around this restriction, but I'll not go into them here and
potentially confuse you even more.
In any of the above cases, it's then a simple matter to retrieve the
value from the $_GET, $_POST or $_SESSION array, as applicable.
Increment the value, and for $_GET or $_POST, place it back in the form.
For $_SESSION, just incrementing the value in the $_SESSION array is
fine. Determine what you want to display with the new value and send it
back to the client.
A bit long but I hope this helps.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|