Link to home
Start Free TrialLog in
Avatar of brucernorton
brucernorton

asked on

Applying a URL variable to a form element

There is no doubt I am overlooking something very obvious, but I just don't seem to get it.  I need to be able to take a url variable and enter it in as the value of a hidden form element.  I have tried both javascript and php and I am just missing something.  The url variable would be 1234 as in http://www.domain.com/index.php?id=1234, or with javascript http://www.domain.com/index.html?id=1234.

I am trying to get that variable into something like
<input type="hidden" name="ID" value=<?php $_get('id') ?> />

What am I doing wrong?
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

OK - the data could have been entered into the URL by GET or by POST - you need to know which. Also the array is $_GET with square brackets and it needs echoing

<input type="hidden" name="ID" value="<?php echo $_GET['id'] ?>" />
A tip for you - $_REQUEST combines many arrays which makes it convenient but unsafe. You can make your own like so

function urlVar( $name ) {

     $result = "";

     if ( isset( $_GET[$name] ) )
          $result = $_GET[$name];
     else
          if ( isset( $_POST[$name] ) )
               $result = $_POST[$name];

     return $result;
}


and then just use $myVar = urlVar("aVariable");

You can even escape the data as well and do other stuff like so  

http://uk2.php.net/manual/en/function.mysql-real-escape-string.php#81530
ASKER CERTIFIED SOLUTION
Avatar of vineethvp
vineethvp
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brucernorton
brucernorton

ASKER

Thank you!