SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling local php with submit

    I have seen where you can open an external php file when clicking on a Submit button in a form, but how can I activate a local php file only when the Submit button is clicked?

    Lawrence

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    What do you mean by 'local php file'? And by 'activate'?

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    What do you mean by 'local php file'? And by 'activate'?
    I have a form and beneath the form I have a php file that loads information into a data base. I don't want the php file to run until the form is complete filled in, and the Submit button is clicked.

    By local, I meant a php file on the same page as the form, not an external php file.

    Lawrence

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In that case, check for the existence of the $_POST array. Put the processing above the form, not below it:
    PHP Code:
    <?php
    $errors 
    = array();
    if (
    $_SERVER['REQUEST_METHOD'] === 'POST'){
        
    //form validation and handling here
        
    if(count($errors) < 1header('location: http://www.yoursite.com/success.php');
    }
    //show the form. You can use the $errors array to inform the user of any errors.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    In that case, check for the existence of the $_POST array. Put the processing above the form, not below it:
    PHP Code:
    <?php
    $errors 
    = array();
    if (
    $_SERVER['REQUEST_METHOD'] === 'POST'){
        
    //form validation and handling here
        
    if(count($errors) < 1header('location: http://www.yoursite.com/success.php');
    }
    //show the form. You can use the $errors array to inform the user of any errors.
    I have other forms on this page with their individual submit buttons. Will this still work?

    It appears this is calling an external php file, I want to run a php file that is located in the same file as the forms. The reason for this is I want to use some other variables located on the same page but not as a result of a form.

    Lawrence

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If you want to post back to the same page (called a postback) make sure your form action looks like this:

    File: index.php
    PHP Code:
    <form method=POST action=index.php name=myform
    OR

    File: something.php
    PHP Code:
    <form method=POST action="" name=myform
    And be aware that if you want to catch whether the form was submitted by clicking a submit button, clicking another image or button and doing being submitted by Javascript OR if a user presses Enter from their keyboard - you will have to do what Arkinstall just said, or look for the value of one of your form elements that should not be empty.

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by lkeeney View Post
    I have other forms on this page with their individual submit buttons. Will this still work?
    If each submit button has to trigger a different php script, then you'll have to give each a unique name or value, and check for it's existance/value.

    It appears this is calling an external php file, I want to run a php file that is located in the same file as the forms.
    Just put your script where it says 'form validation and handling here'.
    The reason for this is I want to use some other variables located on the same page but not as a result of a form.
    Attention! The script is executed before the form is shown to the user. The result of that (a HTML page) is sent to the user's browser. Once the user clicks on the submit button, the form field values are sent back to the server and the script mentioned in the form's "action" parameter is executed (or the same script again if none is mentioned).
    So, any values set in the script the first time, but not saved in some way (hidden fields in the form, session values or whatever), will NOT be there anymore when the form is sent.

  8. #8
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Leaving the action attribute on a form empty is not standard behaviour, and the results are undefined. At least use PHP_SELF in there for a post back.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Use of PHP_SELF was considered harmful. Has that changed?

    Leaving it blank seems to work for me though.

  10. #10
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It does work in most things, but officially the behaviour is undefined.

    Why is PHP_SELF harmful? I've not heard that before. Normally I use my own functions and classes in my framework to get the current page's url anyway.

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    It goes back a few years but reading about PHP_SELF attacks stopped me using it.

  12. #12
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Fair enough. I've never used it myself, but I never leave the action attribute blank either, I have just seen others use it.

    Learn something new every day!

  13. #13
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you can't use "PHP_SELF" for the action, and you can't leave it blank, and it is recommended that you show an action, what is the approved method of submitting the form data to php code listed on the same page as the form?

    A search for "form submit action" on Google provides very little information on this.

    Lawrence

  14. #14
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can either:
    - Have the value hard coded in there
    - Use PHP_SELF but cleanse it first and make sure it doesn't contain anything dodgy
    - I think using one of the $_SERVER variables is ok (SCRIPT_FILENAME or something)

  15. #15
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    - Have the value hard coded in there
    Thats the preferred option, but its less flexible as you are developing, moving files around, refactoring, renaming them etc.

    Plus, in these days of Ajaxified interfaces the form action is becoming less relevant.

    BTW, I am generalising about Admin interfaces, where I tend spend 90&#37; of my time.

    On public webs, with concrete urls almost sacrosanct, then I am pretty sure I hard code the Action without question.

    Perhaps that clarification makes the suggestion to leave it blank makes it seem less radical.

  16. #16
    SitePoint Zealot
    Join Date
    Oct 2008
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i all always submit the PHP files online to test, use the loacal php file it seems no useful

  17. #17
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have used usually $_SERVER['SCRIPT_NAME'] instead of PHP_SELF.

  18. #18
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kikykuang View Post
    i all always submit the PHP files online to test, use the loacal php file it seems no useful
    Unfortunately, I can't find a way to print back to the web page that calls the php file, from within the php file. It just prints on a new page. This is the reason I keep the php file in the web page.

    Lawrence


Bookmarks

Posting Permissions

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