SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,042
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)

    passing query string in a form action to the same page

    I am farly new to the world of serverside scripting and need a bit of assistance.

    Code:
    <form action="<?php echo $_SERVER[ 'PHP_SELF'] .'?some='. $num; ?>" method='post'>
    Is the above possible or is my syntax wrong? The reason I ask is becasue the page I'm working on doesn't error when executed but by $some variable is coming up undefined. So am I just marking it up wrong or is this type of passing not possible?

    I forgot to mention that I have tested for this variable in the part of the script which it resides. It is coming up as it should so that isn't the promblem. Its also defined in the url on the page when it reloads.

    thanks

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You are trying to mix GET (sending parameters through the query string) with POST (sending them through a form with method=post). You have to choose one or the other.

    If you want to send data through a form instead of a link, make "some" a hidden field.

    HTML Code:
    <input type="hidden" name="some" value="<?php echo $num; ?>" />
    And I'll assume when you said $some, you meant $_GET['some'], but with the above form it'll be in $_POST['some'].

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan I have to disagree about the need to choose between GET and POST.
    In the past I've found it helpful to use both. For example on a page that displays and allows the user to edit information with an ID in the query string. The form can just POST back to the page with the ID in the query string and no extra logic is required to determine the ID of the entity being edited.

    Such as:
    Code:
    //now at user.php?id=1
    <form method="post" action="user.php?id=1">
    <input type="text" name="new_username">
    </form>
    Odz - I suspect you just have register globals off (as they should be). This means when the page POSTs you'll need to access $_GET['some'], instead of $some

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If $_GET['id'] is populated in that example, I'd consider it a PHP bug, not a feature, since the HTTP request was POST.

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd never thought of that. I'm not familiar enough with the HTTP spec to know whether this should be considered a bug or not. From a usage POV I find it pretty handy to be able to do this.

    I wonder if other server side languages have the same behaviour?

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,042
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    thanks for the help dan grossman

    cranial-bore wrote:
    Odz - I suspect you just have register globals off (as they should be). This means when the page POSTs you'll need to access $_GET['some'], instead of $some
    yep I know.

  7. #7
    SitePoint Addict
    Join Date
    Oct 2006
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If oddz changes the form's method to get (instead of post), the URL would work. Then the value of some would be accessed in PHP as:

    $some = $_GET['some'];


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
  •