Search code examples
phpdesign-principles

Web programming principles


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!


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 ;)