Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding for back button 2

Status
Not open for further replies.

Itshim

Programmer
Apr 6, 2004
277
US
Hello again,

I have searched the web, asked in other forums (not on tek-tips), and I cannot seem to come up with a good solution for this problem.

I have been coding in PHP for about two years; I have started using OOP (PHP4) and am now developing more complex applications, which actually work as intended :), but one thing still eludes me, how do I handle the user clicking the ‘Forward’, ‘Back’, and ‘Refresh’ buttons?

Originally I was getting into problems because the user would submit (insert) something into the database on one page, and see the results on the subsequent page. If they then click refresh (on that subsequent page) the script would try to insert the information to the database again. To fix this problem I started using a processing page between the two pages, and that works beautifully for refresh, but it causes problems with the back and forward buttons. I have also tried to design interfaces where the user would have no need to use these buttons, but for most users it is just a natural reaction, no matter how many buttons and links you provide for them.

I am not looking for a quick fix or an easy solution, maybe a good tutorial, or even a book. I was wondering if there is a way to design an application which addresses these issues during the planning/development phase, so all the functions of the browser work as expected, but can also be ‘handled’ by the application code.

Thank you for any advise,
Itshim
 
I haven't tried it specifically for that purpose, but have you tried to use sessions?

You could set a session variable with the id from mysql_insert_id() (or the primary key from the record you are inserting), and your confirmation page and final page can check to see if the value in the session array has been posted to the database already (if it exists, it's done!) If they've already posted it, then you can tell them it was done already, and forward them to a different page (if you like.)

Sounds nice in theory, and it just might work.

Those back and forward buttons usually just load the browser's cache, and you can't detect those. You can check set session variables and check them though.
 
I prefer to put all the code into one page, with each part as a function. in the form, there is a hidden id field, which is blank upon entry to the page and filled when the user has completed the form and submitted it.

During the processing, I check for that value, if its there and there is no UPDATE in the form (ie the button) then I know its a refresh and I choose not to process the form...though its simpler and better to send the user onto another page when complete.

Bastien

Cat, the other other white meat
 
Since you have already made your system based on several pages, I would simply make use of sessions.

On top of each of the pages where you need session, or pages that come between the pages that need session, you need to have on the top of the page:

Code:
// let's start the session (on the very top of the php page)
session_start();

Then, you can fill the session variables:
Code:
// fill session variables
$_SESSION['name'] = 'dr feelgood';

If you want compatability with people that deny cookies, the hyperlink and form submits, needs to keep the session id! or the user will be "Logged out" of the session.

Form code, for working without cookies enabled:
Code:
<form action="page.php?<?=SID?>" method="post">

Hyperlink code, for working without cookies enabled:
Code:
<a href="page.php?<?=SID?>" title="go to the next page">next page</a>

How do you then fill your forms with the session data, when user hits "go back"?

Well, in the form elements, you put the session variables:

Code:
echo "<input type=\"text\" name=\"name\" value=\"{$_SESSION['name']}\" />"

This will make the input filled by the session variable!

If you wish to keep the selected items in a list, you simply add a code in the select list:

Code:
if (isset($_SESSION['food'])) {
    $selectedfood = "<option value=\"{$_SESSION['food']}\" selected=\"selected\">Selected: {$_SESSION['food']}";
  }
echo "<select name=\"food\">\n
      {$selectedfood}\n
        <option value=\"Taco\">Taco\n
        <option value=\"Fahitas\">Fahitas\n
        <option value=\"Pizza\">Pizza\n
";

ps. none of the above examples are tested, so they might have typos!

Olav Alexander Mjelde
Admin & Webmaster
 
btw. an addition to my example:
In the select list, add a topmost element:
<option value=\"\">Select one

Then you do in your validation:
If (!isset($food))
{
' halt it
}

I would recommend making some system that halts the registration and tells the user what is lacking!

The topmost element will be chosen by default, if there is no element with selected="selected"

ps. do not use simply SELECTED, as this is not very good for standards..

checked="checked"; not CHECKED.

Anyways, I'm off to lunch, good luck!

Olav Alexander Mjelde
Admin & Webmaster
 
Thank you all for the advice. I am currently using sessions in my application and it will be easy to incorporate this method.

I have just released the application for user acceptance and during this week I will be planning the fixes/upgrades, you have help tremendously.

Thanks Again,
Itshim

PS - DaButcher hope you had a good lunch.
 
Hi again,

No problem, I'm just glad that I could be of help.

Danomac should also have a part of the credit, as he also mentioned sessions..

anyways, My lunch was good. We had cake, coffee, I also had "christmas soda", etc.. (red soda that only is sold around christmas).

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top