patheticsam Posted January 11, 2009 Share Posted January 11, 2009 Hi Guys! I'm just having a little problem..I made a multiple selectbox with some data stored in mySQL database. I just want to be able to define a variable depending what I select in that selectbox without having to process it through a form. As soon as I click on a choice the variable is defined. Is anyone can help me out? Here's the code : <select name="client" size=5 multiple length=30> <?php $db = mysql_connect('localhost', 'username', 'password'); mysql_select_db('database',$db); $sql = "SELECT `field1`, `field2`, `field3` FROM table"; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { echo "<option value=\"".$row['field3']."\">".$row['field1']." ".$row['field2']."\n "; <-----I want to define the variable depending on the choice I make here! } ?> </select> Thanks in advance for your help!! Link to comment https://forums.phpfreaks.com/topic/140391-solved-how-to-define-a-variable-in-a-selectbox/ Share on other sites More sharing options...
abdfahim Posted January 11, 2009 Share Posted January 11, 2009 I think its more Javascript oriented thing. One straight forward thing is you can use OnChange command on that select box and each time you select anything, send that value with GET method, and hence set the php variable from the GET variable. The only problem is the whole page will be reloaded each time. One better procedure is to store that value in a hidden field for later use. But if you want to execute some php command with the new selected item each time user select a new thing, I guess the only possible way is the one I mentioned first. Because, you can't set php variable from javascript. Link to comment https://forums.phpfreaks.com/topic/140391-solved-how-to-define-a-variable-in-a-selectbox/#findComment-734708 Share on other sites More sharing options...
patheticsam Posted January 11, 2009 Author Share Posted January 11, 2009 Thanks I'll look into it. I'll try with the get method cause I've already tried with javascript and just came out with some more problems....Thanks you Link to comment https://forums.phpfreaks.com/topic/140391-solved-how-to-define-a-variable-in-a-selectbox/#findComment-734721 Share on other sites More sharing options...
patheticsam Posted January 11, 2009 Author Share Posted January 11, 2009 What should I set for the onchange value? <select name="client" size=5 multiple OnChange="Value?"> Link to comment https://forums.phpfreaks.com/topic/140391-solved-how-to-define-a-variable-in-a-selectbox/#findComment-734728 Share on other sites More sharing options...
abdfahim Posted January 11, 2009 Share Posted January 11, 2009 <select name="client" size=5 multiple OnChange="window.location='?clt='+this.value"> then you can get the selected item from the following variable echo $_GET['clt']; Link to comment https://forums.phpfreaks.com/topic/140391-solved-how-to-define-a-variable-in-a-selectbox/#findComment-734732 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.