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 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2005
    Location
    New York
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Best way to handle <FORM> result page ?

    Hi guys,

    I have the general setup:

    MyForm.htm // the HTML form
    MyForm.php // the PHP result script

    Everything works fine. I do however want to handle the case where someone goes to MySite.com/MyForm.php without getting there from the MyForm.htm page.

    I tried a hidden field but I get an undefined index error if they don't go from the form page.

    What's the best way to handle this situation ? I'm looking for something clean and not too complicated.

    Thanks.

  • #2
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    on your myform.php file surround all your code in
    PHP Code:
    if ($_POST['Submit'])
    {
      
    // all your code here
    }
    else
    {
      echo 
    "error you cannot view this page";

    if ($_POST['Submit']) just checks to see if your submit button was pressed.. there are many other ways to do it. im sure more people will post

  • #3
    New Coder
    Join Date
    Jul 2005
    Location
    New York
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I get this:

    PHP Notice: Undefined index: Submit in ...

    Now it doesn't work at all, even if I press Submit.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Try something like so:
    PHP Code:
    <?php

    if (!isset($_POST['submit']) OR empty($_POST['submit']))
    {
         echo 
    'You cannot view this page.';
         exit;
    }

    /* processing code here */
    Just name your submit input 'submit'

  • #5
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    rather than just putting an error message, personally I would redirect to MyForm.htm

    PHP Code:
    <?php

    if (!isset($_POST['submit']) OR empty($_POST['submit']))
    {
    header("Location: http://" $_SERVER['HTTP_HOST']
     . 
    dirname($_SERVER['PHP_SELF']). "/" "MyForm.htm");
    }
    Just better for a usability point of view IMO, error messages should be avoided if at all possible

  • #6
    New Coder
    Join Date
    Jul 2005
    Location
    New York
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks everyone.

    I like redirecting also, I just had the 'if' test messed up. It works great now, I'm doing it like Nancy recommended.


  •  

    Posting Permissions

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