Question

Coming from a bit of a conventional (if rusty) programming background, I am busy getting to grips with the "stateless" nature of web sites. It is quite a mindset change!

I've created a small web site for the team in which I work to use internally to track some aspects of our daily grind. The site is functional, I'm pretty proud of what I managed to come up with, bla bla bla.

However I read something somewhere which suggests that I may have done it in a bad way. In particular, the central page of the team website does most of the work. It checks where you came from and then "switches" to perform some work (make some changes in the database) and then again it renders the page.

In many cases the page simply calls itself! What I do is I display a table. In the last column of each row is a set of html forms. Each form have a submit button, at least one hidden field. The "submit" buttons have names/values such as "Delete" "Modify" "Archive" etc.

If $_POST['submit'] == "delete" then I perform that function on a row identified by a hidden field. Vis a vis for "Archive". For Modify I open a new page, display a form with default values, and when the user submits the form the main PHP page is once again called to do an SQL update before it displays the table.

So essentially a large (and growing) case construct near the start of the main page does most of the work, even including the login button!

It seems quite neat and organized this way, but I have two questions:

  1. Is there a way to eliminate the "Resend form data" prompt when a user press Back? The back button doesn't make much sense on this website, most of the time, but we are dealing with humans here. I notice other people have posted similar questions about logout buttons and the like, but the answers I've found so far makes little or no sense to me.

  2. Is this bad programming practice, particularly the whole PHP-calls-itself-from-a-form-action concept .... ?

Thank you for the time!

Was it helpful?

Solution

To your questions:

  1. You are looking for something like Post-redirect-get, that means after submitting you 'do the work' with your data and redirect your user right after, so hitting the back button won't re-submit!

  2. In general that's how it works...in the beginning. After a while you should have a look at OOP (Object oriented programming) or for starters just seperate each action into functions, maybe those functions even into seperate files. Check the PHP Manual for functions or if you are really keen to learn more, OOP would be the holy grail, but that's a 'bit' complex ;)

OTHER TIPS

Post-redirect-get is a pattern that solves this quite cleanly.

What you developed is not a website but more like a web application. In order to eliminate the "back button problem" you should consider using some JavaScript magic(for example Ajax can help you eliminate the need to open another page or to submit to the same page, jQuery can help with displaying and hiding parts oh the page based on user interaction).

For the second question: there is nothing wrong with having all the logic in one php file. Sure, it can hurt if you have something like a bazillion lines of of code but for a small application... why not?!

Speaking generally it is recommended to separate the logic and the GUI. You can read about it here.

It is always a wise idea to redirect the page after the form data has been processed. It avoids resending form on page refresh or back button.

For redirecting in php, you may use:

header("Location: {url here without brackers}");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top