gish Posted November 22, 2008 Share Posted November 22, 2008 hi how do i get href to pass a php variable. I am thinking something like this!! <a href="thesamepage.php" onclick="<?php $variable='this is a variable';?>">wow</a> All I want is the variable become a session variable and then return to the same page but I am drawing a blank =). thanks gish Link to comment https://forums.phpfreaks.com/topic/133730-solved-href-pass-variable/ Share on other sites More sharing options...
gevans Posted November 22, 2008 Share Posted November 22, 2008 The way your trying doesn't involve sessions <?php $variable='this is a variable';?> <a href="thesamepage.php?variable=<?php echo $variable; ?>">wow</a> On the next page <?php $foo = $_REQUEST['variable'] ?> Link to comment https://forums.phpfreaks.com/topic/133730-solved-href-pass-variable/#findComment-695934 Share on other sites More sharing options...
JasonLewis Posted November 22, 2008 Share Posted November 22, 2008 Use $_GET for the URL and $_POST for posting it through forms, avoid using $_REQUEST. It's better to specifically point to what you want to get. Link to comment https://forums.phpfreaks.com/topic/133730-solved-href-pass-variable/#findComment-695978 Share on other sites More sharing options...
gish Posted November 22, 2008 Author Share Posted November 22, 2008 Thanks I have been doing some reading with the difference of opinion. Link to comment https://forums.phpfreaks.com/topic/133730-solved-href-pass-variable/#findComment-696051 Share on other sites More sharing options...
gevans Posted November 22, 2008 Share Posted November 22, 2008 Use $_GET for the URL and $_POST for posting it through forms, avoid using $_REQUEST. It's better to specifically point to what you want to get. Why would you avoid using $_REQUEST? It does the exact same job as $_POST, $_GET and $_COOKIE all under one roof... Link to comment https://forums.phpfreaks.com/topic/133730-solved-href-pass-variable/#findComment-696190 Share on other sites More sharing options...
kenrbnsn Posted November 22, 2008 Share Posted November 22, 2008 It does not do the same thing. It combines all the values into one array and if you have the same index in any of the original arrays, you only get one not all. It's almost as bad as register_globals, since there is no way to verify where the value came from. If you always use the explicit superglobal arrays, you will always know where you've gotten the value. Ken Link to comment https://forums.phpfreaks.com/topic/133730-solved-href-pass-variable/#findComment-696192 Share on other sites More sharing options...
gevans Posted November 22, 2008 Share Posted November 22, 2008 True, but I also know my own code, so I know what cookies have been set, and what data is being sent from forms etc... So I am aware of all the array names and indexes. Obviously $_POST, $_GET and $_COOKIE are different thing, but I never use the same name to declare array variable in them. Link to comment https://forums.phpfreaks.com/topic/133730-solved-href-pass-variable/#findComment-696202 Share on other sites More sharing options...
kenrbnsn Posted November 22, 2008 Share Posted November 22, 2008 You won't, but you don't know what hackers might try. I would recommend always using the appropriate superglobal array. If you're expecting values to come from the URL, use $_GET, values from a posted form (or ajax post, or cURL post) are in $_POST, and cookie values are in $_COOKIE. Also if someone else has to maintain your code (or you come back to it after many years), using the appropriate array will be very helpful when trying to figure out what the code does. -- I've been there. Ken Link to comment https://forums.phpfreaks.com/topic/133730-solved-href-pass-variable/#findComment-696210 Share on other sites More sharing options...
gevans Posted November 22, 2008 Share Posted November 22, 2008 Ok, I see what you're saying. I don't think I'll be making any changes to code I already have, but will take these things into consideration when building in the future. Link to comment https://forums.phpfreaks.com/topic/133730-solved-href-pass-variable/#findComment-696212 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.