3

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!

0

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

oups thanks for that...redesign each time after posting would be a nice feature too, wouldn't it ;)
Thanx Dan. I do use functions, and have an include file for generic site-wide functions. I still need to dip into OOP, the basic theory of which I know. I mean knew. It will all come back to me soon enough.
P.S. I am still trying to make up my mind about whether the redirect method is appropriate in my specific case. I am awarding the point though since I understand the merit of the offered solution in the generic case and since nobody can from the brief description I gave know exactly what my style, preferences and requirements are exactly.
Guess Chris had the best idea here, it involves another language and is client-side which requires the user to have javascript switched on - but (depending on your audiency) propably everyone does. OOP is a whole world by itself, but it's really worth it - just from a developer view where you have to struggle with your own code. Just as a generic example you could write a seperate class just for dynaimcally creating forms...
2

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

1 Comment

Ditto my P.S. comment to Dan - Thank you for the offered solution. I believe it will solve the problem and I will use this method probably many times in the future. In the mean time I am still trying to decide whether it is the best solution for me in THIS instance.
1

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.

1 Comment

I must credit you for good insight into my problem description! Thank you for the suggestions and comments!
1

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

1 Comment

Thank you. Also please see my other responses to other comments!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.