Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to.... in PHP

    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

  • #2
    Regular Coder
    Join Date
    May 2007
    Posts
    100
    Thanks
    16
    Thanked 11 Times in 11 Posts
    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>
    Last edited by >ssp-cdr<; 08-19-2011 at 06:56 PM.

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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 .

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by tangoforce View Post
    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

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Quote Originally Posted by BluePanther View Post
    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:
    PHP Code:
    $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
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by tangoforce View Post
    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:
    PHP Code:
    $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
    haha I think we're describing pretty much the same thing, just in different, ambiguous ways :P


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •