5

I've just started learning PHP and just done with $_POST/$_GET. Now I want to know, what is the pro's and con's of having the PHP to process the data from a form inside the same file, or send the data to another file (action="anotherfile")?

Logically I will think that sending it to another file would increase the time process it, but is that true? When I have the PHP script inside the same file, the page doesnt seem to reload when I hit the submit button (but the content changes). Or does it? If it does, wouldn't the only difference would be that I would have to type the script for the menu (lets say you have the same menu on all pages) in both files? Which would lead to more coding/less space?

saltcracker
  • 321
  • 3
  • 17
  • have you tried to measure it yourself? – Daniel A. White Jan 11 '16 at 11:29
  • 2
    As a hint to your follow-up: Using GET-Parameter makes a link easily shareable. Have a look at how google maps stores location information in the Url, or imagine sharing a product with someone else via a link like someshop.com/?product=fancyproduct – T3 H40 Jan 11 '16 at 11:29
  • 2
    @T3H40 but you really have to ensure to share no sensitive data. – shock_gone_wild Jan 11 '16 at 11:32
  • T3 H40: That is excactly what I thought, that it is more userfriendly to link/share when the data is not supposed to be secure at all. Thanks! – saltcracker Jan 11 '16 at 11:33
  • 1
    @shock_gone_wild Yes! That is important. GET parameters make *sharing* information easy - so don't put anything there that you don't want to share – T3 H40 Jan 11 '16 at 11:33
  • "Follow-up question" — If you have another question, than [ask](http://stackoverflow.com/questions/ask) another question, and then watch it be closed as [a duplicate](http://stackoverflow.com/questions/504947/when-should-i-use-get-or-post-method-whats-the-difference-between-them) – Quentin Jan 11 '16 at 11:36
  • Quentin: I've removed it. – saltcracker Jan 11 '16 at 11:38

4 Answers4

10

what is the pro's and con's of having the PHP to process the data from a form inside the same file, or send the data to another file (action="anotherfile")?

You are conflating files and urls.

By having the logic split between different files (and then included where appropriate) you seperate concerns and make your code easier to manage.

By having a single URL be responsible for both displaying the form and processing the form data you don't end up in the awkward situation where the result of processing the form data requires that you redisplay the form with error messages in it. If you used two different URLs there you would need to either display the form on the processing URL (so you have two different URLs which display the form) or perform an HTTP redirect back to the original URL while somehow passing details of the errors to it.

Logically I will think that sending it to another file would increase the time process it, but is that true?

No. It makes no difference on the time scales being dealt with.

When I have the PHP script inside the same file, the page doesnt seem to reload when I hit the submit button (but the content changes).

It does reload.

If it does, wouldn't the only difference would be that I would have to type the script for the menu (lets say you have the same menu on all pages) in both files?

That's what includes are for.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

In any language we always try to write clean code. That's why we follow MVC.

Logically I will think that sending it to another file would increase the time process it, but is that true? I think NO.

Because when we send data to another page and on another page at the top we echo that post data and exit. you will see it will not take time. it take time when we redirect/load some html page after that.

It does not matter where we sending data (same page or another page). matter is what is loading after that.

Monty
  • 1,110
  • 7
  • 15
2

There is no difference about speed.

Whetever you post the content of your form in standard submit, this data will be sent to the server and a response (after processing ) will be downloaded.

The only difference is about organization of your code. The logic that draws themplate of page (menu or other fixed parts) should be stored in some file that you can include separately or call by a function.

Is also true that when you post your data you do for some reason, register a user for example. Is a good pratice that the php file that handles user registration will do that and output the messages and not other functions. If your file has some logic switches that make it output either an empty form or a a registration message based on the presence of post or get variables, you will notice that when you scale to more complex tasks this will add complexity and make code mantainment harder.

AndreaBogazzi
  • 14,323
  • 3
  • 38
  • 63
  • Thanks! I started re-organizing the website earlier today. Created a template directory for some of the DIVs and the menu. The book I have use "inc" (probably meaning "include") in the names of the files which are included into other files, for example "menu.inc.php". Is that something that has become a standard in the PHP community or is it just an example in the book? – saltcracker Jan 11 '16 at 11:44
  • 1
    I just put them in folder called include, i really do not know. Normally you have at least sessio n security managment and database connection, plus templates. I usually have one main include for pages meant to be viewed and another smaller for pages loaded with ajax techiniques. – AndreaBogazzi Jan 11 '16 at 11:51
  • Oh I see! I don't see what I would use the session and security management directories for right now though, but probably later on when I learn more about the language (and SQL/databases, one hell of a book, which I learn in another course). I will definetly have to organize my webserver better since I get kinda lost sometimes, cause the code/scripts starts to get really big. – saltcracker Jan 11 '16 at 12:08
1

I'll try to make sure I understand your question by restating it.

If you have a form (/form.php), and the "action" of that submit button leads you to a separate php page (/form_action.php), there is absolutely no difference in speed. Each HTTP request (form.php and form_action.php) is independent - "form_action.php" doesn't remember anything about "form.php" unless you pass that information through (as parameters). This is what people mean when they say that HTTP is stateless. It's worth learning about how HTTP works in general alongside the details of PHP.

If you have a PHP script which in turn includes other PHP scripts, there is a tiny performance impact - too small to measure in pretty much any case I've ever come across.

However, using includes allows you to separate your markup (the HTML) from the logic (the PHP). This is a really good thing if you are doing anything other than tinkering. It allows you to re-use functionality, it makes it much easier to change and maintain the code over time, and it helps you think through what you're trying to achieve.

There are many different ways people have solved the "how do I keep my code clean" puzzle; the current orthodoxy is "Model-View-Controller" (as @monty says). There are also PHP frameworks which make this a little easier to implement - once you've got the basics of the language, you might want to look at Zend or TinyMVC (there are several others, each with their benefits and drawbacks).

Community
  • 1
  • 1
Neville Kuyt
  • 29,247
  • 1
  • 37
  • 52
  • Yes, you understand my question. I will definetly look on how HTTP (and other protocols) works. I actually thought about that yesterday. To seperate the HTML and the PHP if the PHP script could be reused several times with include. I'll look into the MVC, thanks man! – saltcracker Jan 11 '16 at 12:00