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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    42
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Question how to handle form action url with parameter?

    Hi..

    I need to process value from a form handled by parameter.
    I write these code:

    PHP Code:
    <form action="<? echo $_SERVER['PHP_SELF']; ?>?id=1&action=add" method="post">
    <input type="text" name="name" />
    <input type="text" name="age" />
    <input type="submit" value="Send" />
    </form>

    <?php

    if ($id == "1") {
       if (
    $action == "add") {
       echo 
    $_POST['name'];
       echo 
    $_POST['age'];
       }
    }

    ?>
    But, I got blank result when submit button is clicked.

    Please help me..

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You need to use $_GET['id'] & $_GET['action'] for the variables in your action url.

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    42
    Thanks
    11
    Thanked 0 Times in 0 Posts
    if i use $_GET['id'] & $_GET['action'] so i should use method="get", isn't it?
    I've use GET method but it has blank result too if I implemented it with form.

    How to correct this? Could you fix my code?
    and is my method to catch values by using 'if' correct?

    I need suggestions. Please help me.

    Thank you so much before.

  • #4
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    PHP Code:
    if ($_GET['id'] == "1") {
       if (
    $_GET['action'] == "add") {
       echo 
    $_POST['name'];
       echo 
    $_POST['age'];
       }

    The ?foo=bar part of a URL is actually a GET request, and you probably have register_globals off (or you should have) so $id and $action won't be in the global namespace.

    You do not need to change your action URL as suggested above.
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini

  • Users who have thanked Ahri for this post:

    frozenade (11-27-2007)

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    As Ahri suggests, your code is written assuming register_globals is on, and you're probably on a server where register_globals is off.

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by Inigoesdr View Post
    You need to use $_GET['id'] & $_GET['action'] for the variables in your action url.
    One could also use $_REQUEST instead of $_GET.

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Quote Originally Posted by frozenade View Post
    if i use $_GET['id'] & $_GET['action'] so i should use method="get", isn't it?
    The method parameter for the form only affects how the inputs inside of the form are submitted.
    Quote Originally Posted by aedrin View Post
    One could also use $_REQUEST instead of $_GET.
    One could; but it would be overwritten if you had a field with the same name in the form, or cookie, or session.

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by Inigoesdr View Post
    One could; but it would be overwritten if you had a field with the same name in the form, or cookie, or session.
    Actually, it does not include the session variables. Which only leaves Cookies to mess things up, which generally doesn't (and shouldn't - since they should be used sparsely) happen.

    It also reinforces the fact that one should properly name their variables/names. Using identifiers of less than 3 characters is generally bad, unless it's for a loop or counter.

  • #9
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by aedrin View Post
    It also reinforces the fact that one should properly name their variables/names. Using identifiers of less than 3 characters is generally bad, unless it's for a loop or counter.
    That is true in most cases. In some cases, a simple $id would be more efficient than something like $action_id or $user_id. After all, sometimes the actions are driven via different pages. That makes $action_id useless. Sometimes you don't have a $msg_id or $ticket_id because it isn't a part of a forum or ticket system or whatever. That might leave only $user_id, which would be less efficient than just $id.

    However, I should add that there is a noteworthy counterargument - consistency between scripts. If you have a forum integrated with your site, using $user_id, $msg_id or whatever else might be used is typically better than just $id because the variable explains the contents of the variable, reducing the need for commenting, and because of the fact that a lack of consistency can lead to unnecessary confusion.

    Whichever side you choose, be aware of the disadvantages.
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello

  • #10
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Just to clarify the issue with your original code... you have specified the method="post". So you will access your form elements (name, age, and send) as $_POST['name'], etc. Since you have concatenated the other query strings onto the action (id=1&action=add), those are passed as "GET" variables and you would have to access them as $_GET['id'] and _$GET['action'].

    The following would work.

    PHP Code:
    <form action="<? echo $_SERVER['PHP_SELF']; ?>?id=1&action=add" method="post">
    <input type="text" name="name" />
    <input type="text" name="age" />
    <input type="submit" value="Send" />
    </form>

    <?php

    if ($_GET['id'] == "1") {
       if (
    $_GET['action'] == "add") {
       echo 
    $_POST['name'];
       echo 
    $_POST['age'];
       }
    }

    ?>

  • Users who have thanked arnyinc for this post:

    frozenade (11-27-2007)

  • #11
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Hmm.. I thought that was pretty clear. The code you posted, and the explanations were already stated.

  • #12
    New Coder
    Join Date
    Jun 2007
    Posts
    42
    Thanks
    11
    Thanked 0 Times in 0 Posts
    wow.. thank you so much for help me out..

    i very very thank you..
    now i can get the values for user input.
    thank you once again..

    codingforums is

  • #13
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Apparently he was just looking for fixed code to copy/paste.

  • #14
    New Coder
    Join Date
    Jun 2007
    Posts
    42
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Unhappy

    Quote Originally Posted by Fumigator View Post
    Apparently he was just looking for fixed code to copy/paste.
    oops, no Mr. Fumigator, please don't be sarcastic. i'm just trying to find a solution to fixing my code. I am a newbie and I write the code my self then I got that problem. It's pure for me to need some helps, not just for copy paste.

  • #15
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Especially if someone is learning one shouldn't get the answer spelled out for them. If you learn by reading what someone suggests to you, but never create yourself, you become stuck in a pattern where you might as well start learning a new language. Because it makes you not very useful.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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