Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP in same file as form or seperate? Speed

Tags:

php

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?

like image 805
saltcracker Avatar asked Jan 11 '16 11:01

saltcracker


2 Answers

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.

like image 68
Quentin Avatar answered Sep 22 '22 19:09

Quentin


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.

like image 27
Monty Avatar answered Sep 21 '22 19:09

Monty