realjumper Posted April 24, 2008 Share Posted April 24, 2008 Hi, I hope someone can shed some light on this problem....it seems a simple task but I just can't figure it out. Here's my code: <script type="text/javascript"> function showSelected(val){ document.getElementById ('selectedResult').innerHTML = "" + val; } </script> <?php $query12 = mysql_query("Select id,ringi_num,ringi_count FROM ringi_details WHERE sid = $sid") echo "<select name='test' onChange='showSelected(this.value)'>"; echo "<option value=''>Ringi Not Required</option>"; while ($r = mysql_fetch_array($query)) { $ringi_num = $r["ringi_num"]; $ringi_count = $r["ringi_count"]; $ringi_num = "$ringi_num $ringi_count"; $id = $r["id"]; echo "<option value='$id'>$ringi_num</option>"; } echo "</select>"; echo "<div id='selectedResult'></div>"; ?> Now that all works just fine....when I echo '<div id='selectedResult'></div>' it returns the value of the selected item in the drop-down....perfect! But, how do I get the value into a variable? For example, when I select an option from the drop-down it might return (say) 267 when I echo '<div id='selectedResult'></div>'. What I want is to have the returned value available to use as a variable in another query. In laymans terms I want: $number = '<div id='selectedResult'></div>'; // where '<div id='selectedResult'></div>' = (say) 267 If that is possible, then I can use $number in the WHERE condition of my next query. Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/ Share on other sites More sharing options...
realjumper Posted April 24, 2008 Author Share Posted April 24, 2008 If the above is not possible...is there another way that I can get the value of the selected item from the drop down into a variable? Quote Link to comment https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/#findComment-525692 Share on other sites More sharing options...
mrdamien Posted April 24, 2008 Share Posted April 24, 2008 Edit: Wait, silly me. Just make a form around your select box, then submit it. The value will be available through $_POST or $_GET Quote Link to comment https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/#findComment-525713 Share on other sites More sharing options...
realjumper Posted April 24, 2008 Author Share Posted April 24, 2008 Yes that would work but I don't want to refresh the page or else all the other data the user puts in other fields will be lost Quote Link to comment https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/#findComment-525722 Share on other sites More sharing options...
mrdamien Posted April 24, 2008 Share Posted April 24, 2008 Then you could either use an iFrame (which doesn't usually work properly), or look into ajax Quote Link to comment https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/#findComment-525729 Share on other sites More sharing options...
realjumper Posted April 24, 2008 Author Share Posted April 24, 2008 Yup...I've been looking into ajax all day, but I can find an example that helps me Quote Link to comment https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/#findComment-525730 Share on other sites More sharing options...
DarkWater Posted April 24, 2008 Share Posted April 24, 2008 Use Ajax to send the info to PHP and get the information back. =P Quote Link to comment https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/#findComment-525732 Share on other sites More sharing options...
realjumper Posted April 24, 2008 Author Share Posted April 24, 2008 Use Ajax to send the info to PHP and get the information back. =P Yeah...sounds easy but an example would be great....I've been searching and searching......and searching and searching..... Quote Link to comment https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/#findComment-525733 Share on other sites More sharing options...
mrdamien Posted April 24, 2008 Share Posted April 24, 2008 You could make your ajax functions by yourself, but try using a javascript library like jquery. Other libraries include (google) mootools / prototype Once you download/include the jquery library do something like this: <script> function myFunction(id){ $.get("page.php", { drink: id }, function(data){ alert("Data Loaded: " + data); }); } </script> <option onchange="myFunction(this.value)"> <select value="1">water</option> <select value="2">soda</option> <select value="3">beer</option> </option> <? #page.php switch($_GET['drink']){ case 1: echo "water is okay"; break; case 2: echo "yay for soda"; break; case 3: echo "nice"; break; } ?> This wasn't tested, but should give you the idea. Quote Link to comment https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/#findComment-525734 Share on other sites More sharing options...
realjumper Posted April 24, 2008 Author Share Posted April 24, 2008 Okay...I'll check it out...thank you Quote Link to comment https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/#findComment-525737 Share on other sites More sharing options...
realjumper Posted April 27, 2008 Author Share Posted April 27, 2008 Okay....over the weekend I had a play with Ajax and in the end I got a 'test' page to do exactly what I was seeking. I found 'prototype.js' worked really well, so now I will apply this to my original problem. If anyone is interested I can post the completed code....once I have completed it!! Thanks to those who helped :-) Quote Link to comment https://forums.phpfreaks.com/topic/102644-solved-phpjavascript-help-please/#findComment-528497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.