SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question form action="" and form action="?"

    Hello,

    I am reading "Build Your Own Database Driven Web Site, 4th Edition".

    In chapter 3 (page 109) the book introduces a rather strange way to use a form action.
    <form action="" method="post">
    The book further explain that by sending an empty action, the form is submitted to the same PHP script.

    In chapter 4 (page 132) the book introduces:
    <form action="?" method="post">
    This time the book explains that by using action="?" any data after the ? in the url will be deleted, basically deleting and GET information.

    I am a bit confused as I cannot find any documentation around explaining these two ways of submitting a form. Can anyone point me on the right explanation/documentation in the php.net website?

    Cheers
    Luca

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    This is not something specific to PHP, but general behavior of forms.
    Indeed as you say, when you leave the action blank the form will be posted to the URL the user is currently on.

    So for example if the user is on http://blah/?a=1&b=2&c=3, then that's where the form will be submitted.

    Now, if you fill in anything in the action that is not a full URI (i.e. does not start with a schema like http://), it will be treated as a relative URL.
    So for example, if I fill in "?" as action on a form on page http://blah/?a=1&b=2&c=3, it will post to location "?" relative to that URL, which is http://blah/? (note that the ? at the end here is from "?" in the action, it is not the ? that was already there. The ? that was there originally was removed, and this one was put in its place).

    A few more examples to hopefully make this a bit clear:

    http://blah/blah?a=1, with form action "", will post to http://blah/blah?a=1

    http://blah/blah?a=1, with form action "?", will post to http://blah/?

    http://blah/blah/?a=1, with form action "", will post to http://blah/blah/?a=1

    http://blah/blah/?a=1, with form action "?", will post to http://blah/blah/?

    http://blah/blah/?a=1, with form action "/", will post to http://blah/

    Does that make sense?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi ScallioXTX,

    Yes, It starts to make sense. Does it means that:

    http://blah/blah1/blah2?a=1, with form action "..", will post to http://blah/blah1

    In other words can I put any shell navigation command as an action?

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now that I am thinking, could you put directly PHP code in the form action? Something like:
    <form action="<?php ...php code... ?>" method="post">

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Re ".." , almost, but not quite how you did it, it should end in a / , i.e., "../". I personally wouldn't use this though as it may (and will) confuse people.

    Re using php in action="", you sure can

    A pattern that is used a lot these days is:

    • Post to current URL (i.e., action="")
    • Check if the input is valid,
      • if it is: redirect to another page (redirect to make sure you don't get the "Resubmit form" error from the browser when you press the back key)
      • if it's not: just show the page again and indicate the errors made on the form (by showing error messages and making the labels of erroneous fields red for example)


    I use this all the time and it works perfectly well. The only reason I can think of where it would use an action other than "" is if I had to post some data to somebody else's website in the browser for whatever reason (and indeed, that almost never happens).
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ScallioXTX,

    Great thanks for the crystal clear explanation.

    Thanks Again
    Luca


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
  •