PHP get url PARAMETER into form

Discussion in 'PHP' started by matty281k, May 1, 2008.

  1. #1
    If i have form where i have a value like this value=""

    so there is no value

    i need to get the "3435435" value from the url like below.
    http://www.somesite.com/blahblah&_moon=3435435

    then add it into the FORM value so when the form submits its like this

    value="3435435"

    any ideas?
     
    matty281k, May 1, 2008 IP
  2. graham23s

    graham23s Well-Known Member

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #2
    Not sure i follow but i think you want to grab the value form the address bar?

    no probs, you would use a $_GET like so:

    www.somesite.com/example_id=123&another_example_id=456
    PHP:
    on the page just do:

    
    $id_1 = $_GET['example_id'];
    $id_2 = $_GET['another_example_id'];
    
    // testing we caught the ids //
    print("The example id: $id_1 and the another example id is: $id_2");
    
    PHP:
    thats the basics of it, there more secure ways to do the above though, hope thats a help to you.

    Graham
     
    graham23s, May 1, 2008 IP
  3. Randombase

    Randombase Peon

    Messages:
    224
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In the actual code it would be:
    
    value="<?=$_GET['_moon'];?>"
    
    PHP:
    However, in that state it's vulnerable to Cross Site Scripting (XSS) so I'd recommend outputting the variable through htmlspecialchars() ;).
     
    Randombase, May 1, 2008 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Within the <form> tag in your HTML code, insert:

    
    <input type="hidden" name="userid" id="userid" value="3434">
    
    Code (markup):
    And then in the PHP code, you get the value:

    
    echo $_POST['userid']/$_GET['userid'];
    //Depending on form type
    
    PHP:
    Peace,
     
    Barti1987, May 1, 2008 IP
  5. matty281k

    matty281k Active Member

    Messages:
    270
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Yes but _moon=34433

    is on a different url from the FORM

     
    matty281k, May 1, 2008 IP
  6. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #6
    Did you mean,
    the user will input an URL (for example "http://www.somesite.com/blahblah&_moon=3435435") to your form,
    and your script should print "3435435" ?
     
    xrvel, May 1, 2008 IP
  7. matty281k

    matty281k Active Member

    Messages:
    270
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Nope theres a url "http://www.somesite.com/blahblah&_moon=343543" that always has _moon value with a different location to the FORM
     
    matty281k, May 2, 2008 IP
  8. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #8
    I dont understand what you want. the solution posted by Randombase is close, although I would do this:

    use htmlentities else your conna get some XSS attacks going on.
     
    lanmonkey, May 2, 2008 IP
  9. matty281k

    matty281k Active Member

    Messages:
    270
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #9
    ok your saying get _moon, that would get _moon from the current url

    i need it from a different url that the form is on
     
    matty281k, May 2, 2008 IP
  10. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #10
    
    $url = 'http://www.somesite.com/blahblah&_moon=3435435';
    
    $QUERYVAR= parse_url($url, PHP_URL_QUERY);
    
    $GETVARS = explode('&',$QUERYVAR);
    
    foreach($GETVARS as $string){
         list($is,$what) = explode('=',$string);
         echo "$is -> $what<br/>";
    }
    
    PHP:
    Peace,
     
    Barti1987, May 2, 2008 IP