FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » button, parameter and reload - How do I glue it all together?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
button, parameter and reload - How do I glue it all together? [message #178785] Fri, 03 August 2012 14:29 Go to next message
mwoolley is currently offline  mwoolley
Messages: 2
Registered: August 2012
Karma: 0
Junior Member
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
Re: button, parameter and reload - How do I glue it all together? [message #178786 is a reply to message #178785] Fri, 03 August 2012 15:23 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
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?
>
By making the button perform a submit action - either becuse its a
submit button, or its a url button that calls the page with a parameter
set as a GET or because its hooked to javsacript that causes a submit
and sets parameters on the way.

In general your PHP code should be of the form

IF
(page is to be displayed as a result of a submit action )
THEN
(update any persistent data like a database with the users
entry data contained in the POST variables)

(get the parameters and make up the display according
to what they are )
ELSE
(display an initial page reflecting no user input yet)


($SESSION is a use of cookies by the way. Its just cookies wrapped in a
simple API.)


The only way you can do this without a submit action is to downland ALL
the possible screen options in e.g. a javascript array and display the
selection you want depending on buttons hooked to javascript functions.

You would typically do that if you wanted to give the user something
like an online calculator or some similar app.

Whereas posting data and reloading the page is more appropriate as a
forms front end to a database for example.

You can mix the two, but it gets complicated. I have done that to create
huge menu systems reflecting a hierarchy of parts in a stock database.
ALL the parts are downloaded and they are only revealed when the user
navigates a selection of drop down menus.




> I hope this make sense; the old grey matter is addled today!
>
> Thanks in advance.
> Martin


--
To people who know nothing, anything is possible.
To people who know too much, it is a sad fact
that they know how little is really possible -
and how hard it is to achieve it.
Re: button, parameter and reload - How do I glue it all together? [message #178787 is a reply to message #178786] Fri, 03 August 2012 15:35 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 8/3/2012 11:23 AM, The Natural Philosopher wrote:
<snip>
>
>
> ($SESSION is a use of cookies by the way. Its just cookies wrapped in a
> simple API.)
>

Completely INCORRECT!

>
>
>> I hope this make sense; the old grey matter is addled today!
>>
>> Thanks in advance.
>> Martin
>
>


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
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 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
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
==================
Re: button, parameter and reload - How do I glue it all together? [message #178791 is a reply to message #178785] Sat, 04 August 2012 06:00 Go to previous messageGo to next message
Michael Vilain is currently offline  Michael Vilain
Messages: 88
Registered: September 2010
Karma: 0
Member
In article <ec8466e1-7d98-4750-bc1a-c1c11e0d7c2a(at)googlegroups(dot)com>,
mwoolley <trogoz2012(at)gmail(dot)com> 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


So the logic of the php script, as I understand it is

script is called without any sort of argument in the URL and generates a
page.

The value for the variable is obtained through a $SESSION variable or
some default. A form with a button is displayed, possibly with a number
showing the value of the autoincrement variable.

Click on the button, which must be part of a form. The form submits the
page using the script $_SERVER['PHP_SELF'], passing the autoincremented
variable as a $_GET variable.

The resulting page displays, determines that $_GET[variable] is set, and
uses that value rather than the session

I don't see how reloading the page comes into this. When you click the
<button>, that should submit the form and redraw the page. a <button>
tag That's usually used for "hands off" automatic reloading.

If this isn't part of the logic you're trying to accomplish, correct me
where I don't have it right.

--
DeeDee, don't press that button! DeeDee! NO! Dee...
[I filter all Goggle Groups posts, so any reply may be automatically ignored]
Re: button, parameter and reload - How do I glue it all together? [message #178801 is a reply to message #178785] Mon, 06 August 2012 12:47 Go to previous messageGo to next message
mwoolley is currently offline  mwoolley
Messages: 2
Registered: August 2012
Karma: 0
Junior Member
Brilliant replies! Thanks for all your help! I managed to get it all to work by creating the button (two actually) with this code

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="image" name="fbb" value="1" src="cal-bck.png" alt="bck" />
<input type="image" name="fbf" value="1" src="cal-fwd.png" alt="fwd" />
</form>

and the magic to see which button was pressed, and take appropriate action

if (isset($_POST['fbb']) or isset($_POST['fbb_x'])) {
build the backwards
} else {
if (isset($_POST['fbf']) or isset($_POST['fbf_x'])) {
build the forwards
}
}
Re: button, parameter and reload - How do I glue it all together? [message #178802 is a reply to message #178801] Mon, 06 August 2012 16:59 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Mon, 06 Aug 2012 05:47:44 -0700, mwoolley wrote:

> if (isset($_POST['fbb']) or isset($_POST['fbb_x'])) {
> build the backwards
> } else {
> if (isset($_POST['fbf']) or isset($_POST['fbf_x'])) {
> build the forwards
> }
> }

Suggestion:

if ( forwards_condition ) {
do the forwards thing
}
elseif ( backwards_condition ) {
do the backwards thing
}
else {
do the nothing thing
}

You don't need to use an if inside the else, elseif (or else if) allows
you to split a decision more than two ways without nesting if statements,
and in this case would be more logical.

Rgds

Denis McMahon
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: App2Fame is back: Bigger, Better & Loads of Prizes.
Next Topic: Handling with xmlhttp.responseText
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 03:05:39 GMT 2024

Total time taken to generate the page: 0.02732 seconds