...

View Full Version : How to.... in PHP



hshourie
08-19-2011, 05:29 AM
Hi

When we write code for form element where we take inputs from the user and then using POST method to retrieve information.

Best way to using POST method is
on same form or separate means where we code for input, is that ok that we retrieve information there itself or should we code in other file.

Thanks
Himanshoo

>ssp-cdr<
08-19-2011, 07:07 AM
What I usually do is in the FORM tag set the action to be action="?". That will make the form submit to the same page. Then at the beginning of your script for that page do this with PHP:


if ($_SERVER['REQUEST_METHOD'] == 'POST') { // handle post... }


Often what I do is this:


if ($_SERVER['REQUEST_METHOD'] == 'POST') require('post.php');

This way you can do all the processing of the form data in a separate file. If everything is valid then you can forward the user to a new page and exit, or let the script continue back to the main script and do something else from there. Also, if the form data is invalid then continue back to the main script and display the form again with the necessary error message.

I recommend using an .htaccess file (Apache required) to block anyone from accessing the post.php file directly. This code in such a file will do that:


<Files ~ "^post\.php$">
Order allow,deny
Deny from all
</Files>

BluePanther
08-19-2011, 09:05 AM
Personally, I prefer separating my logic from my controller. There's certain framework models you could look at to get really into this sort of thing. MVC (Model View Controller) type architectures completely separate the logic, processing and what the user sees. You should look into it.

But, yeah as I was saying. I prefer form actions to go to a separate parsing script. It helps keep things tidier imo and tidy code is good code :).

tangoforce
08-19-2011, 03:02 PM
Ah now see thats where you and I differ somewhat BP. I run EVERYTHING through one script. Sure I have html templates scattered around for different pages but my main script is where everything runs through. IT deals with session checks, checks user logins, checks $_GET/$_POST submissions etc. Everything is in one place but it does include some other .php files where I've got self written functions etc.

Doing it all in one script makes things far easier to keep track of. Say you've set a few session variables and you got 30+ scripts which might also need to rely on them.. thats a lot different files you may potentially need to update (even if you use an include - suppose the file path changes?). Thats why I run everything through the one central script which then includes others as and where needed. This is how a lot of the big scripts like invisionboard also work.

BluePanther
08-19-2011, 05:35 PM
Ah now see thats where you and I differ somewhat BP. I run EVERYTHING through one script. Sure I have html templates scattered around for different pages but my main script is where everything runs through. IT deals with session checks, checks user logins, checks $_GET/$_POST submissions etc. Everything is in one place but it does include some other .php files where I've got self written functions etc.

Doing it all in one script makes things far easier to keep track of. Say you've set a few session variables and you got 30+ scripts which might also need to rely on them.. thats a lot different files you may potentially need to update (even if you use an include - suppose the file path changes?). Thats why I run everything through the one central script which then includes others as and where needed. This is how a lot of the big scripts like invisionboard also work.

I think it's all personal preference really. I've trained myself to separate logic from display, which is why I prefer MVC type frameworks. Joomla very much adheres to MVC (apart from some of the community-made extensions) and I think it benefits greatly from it.

But, keeping everything in single files has it's advantages too. All just preference :)

tangoforce
08-19-2011, 06:48 PM
I've trained myself to separate logic from display,

Ah, now I think perhaps you're getting confused with what I'm saying. All my display (html) is in seperate files but they're only templates. They're tagged up so that the templates can have logic generated contented put into them before being printed out to the browser. The actual php code itself doesn't contain any display related code just PHP doing its back end stuff. Thats the way I prefer to code.

I wrote a contact form processing script for someone recently and did the same thing there too. I didn't like the idea of this:


$Message = "
Hello $User,

Thanks for your form saying:
$Message

I'll be in touch";


Horrible idea.. no, instead I put the email text into .txt files as templates so they can be edited. It's then read out of the file, variables put in and then sent. Much cleaner :thumbsup:

BluePanther
08-19-2011, 08:04 PM
Ah, now I think perhaps you're getting confused with what I'm saying. All my display (html) is in seperate files but they're only templates. They're tagged up so that the templates can have logic generated contented put into them before being printed out to the browser. The actual php code itself doesn't contain any display related code just PHP doing its back end stuff. Thats the way I prefer to code.

I wrote a contact form processing script for someone recently and did the same thing there too. I didn't like the idea of this:


$Message = "
Hello $User,

Thanks for your form saying:
$Message

I'll be in touch";


Horrible idea.. no, instead I put the email text into .txt files as templates so they can be edited. It's then read out of the file, variables put in and then sent. Much cleaner :thumbsup:

haha I think we're describing pretty much the same thing, just in different, ambiguous ways :P



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum