undertaker Posted November 9, 2011 Share Posted November 9, 2011 I would like to fetch data from database when item is selected from dropdown list and show it instantly without refreshing the site. Sample: <select> <option> something </option> <option> something2 </option> <option> something3 </option> </select> <div>shows mysql database results here, depends on item we have chosen</div> Quote Link to comment https://forums.phpfreaks.com/topic/250767-dropdown-database-select/ Share on other sites More sharing options...
undertaker Posted November 9, 2011 Author Share Posted November 9, 2011 Oh and is there a way to do it in association with PHP? So the database password cant be visible in online source code? Quote Link to comment https://forums.phpfreaks.com/topic/250767-dropdown-database-select/#findComment-1286582 Share on other sites More sharing options...
thomasw_lrd Posted November 9, 2011 Share Posted November 9, 2011 Something like this could work for($x=0; $x<=$items; $x++) { $id=$result[$x]['pol_id']; $thing=$result[$x]['pol_policy_number']; $options.="<OPTION VALUE=\"$id\">".$thing; } ?> Select a case number to see the payout approval. <br> <form action="approval.php" method='post'> <SELECT NAME=pol_policy_number> <?=$options?> </SELECT> <input name="Submit" type="submit" Value="Submit Policy Number"/> </form> <br> You'll have to put the count of an array in $items. And change the Policy information to match your database. As to your second quetions, Just use an include statement like include security.inc Security.inc would consist of the username and password. Quote Link to comment https://forums.phpfreaks.com/topic/250767-dropdown-database-select/#findComment-1286588 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2011 Share Posted November 9, 2011 Just use an include statement like include 'security.inc'; Security.inc would consist of the username and password. ^^^ I hope you are not using that, because if you browse to the .inc file, you can see the username and password. You need to use a .php file so that the php code defining the username/password will not be literally output if someone browses to the file. Quote Link to comment https://forums.phpfreaks.com/topic/250767-dropdown-database-select/#findComment-1286593 Share on other sites More sharing options...
thomasw_lrd Posted November 9, 2011 Share Posted November 9, 2011 I was not aware of that. I inherited this system from someone who is much smarter than I am. I'm not even sure what the passwords to our system are. I did browse to the link though, and it worked. Luckily, our passwords were not stored there. Our config files for the website are actually stored in /etc/something/something. Quote Link to comment https://forums.phpfreaks.com/topic/250767-dropdown-database-select/#findComment-1286597 Share on other sites More sharing options...
tomfmason Posted November 9, 2011 Share Posted November 9, 2011 I would like to fetch data from database when item is selected from dropdown list and show it instantly without refreshing the site. Sample: <select> <option> something </option> <option> something2 </option> <option> something3 </option> </select> <div>shows mysql database results here, depends on item we have chosen</div> What you are wanting is a simple ajax request that fires on the select's onSelect event. I would recommend using jquery or some other related javascript framework. Writing this using jquery would be very easy. For example: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#select").change(function(){ $.ajax( "file.php?selected=" + $(this).val(), "success":function(data){ $("#results").html(data); } ) }) }); </script> <select id="select"> <option> something </option> <option> something2 </option> <option> something3 </option> </select> <div id="results">shows mysql database results here, depends on item we have chosen</div> Whenever you select a new option in the drop down an ajax GET request will be sent to file.php with the value of that option in the GET param "selected". In the file.php you would access it via $_GET['selected'] @thomasw_lrd You forgot got the close the option tags in the code you posted. Quote Link to comment https://forums.phpfreaks.com/topic/250767-dropdown-database-select/#findComment-1286602 Share on other sites More sharing options...
thomasw_lrd Posted November 9, 2011 Share Posted November 9, 2011 @thomasw_lrd You forgot got the close the option tags in the code you posted. I copied the code from my production server. Can you explain a little further? It's working, so I'm not sure what I didn't close, but I would like to fix it. Not trying to highjack the thread by the way, just trying to learn. Quote Link to comment https://forums.phpfreaks.com/topic/250767-dropdown-database-select/#findComment-1286618 Share on other sites More sharing options...
tomfmason Posted November 9, 2011 Share Posted November 9, 2011 @thomasw_lrd You forgot got the close the option tags in the code you posted. I copied the code from my production server. Can you explain a little further? It's working, so I'm not sure what I didn't close, but I would like to fix it. Not trying to highjack the thread by the way, just trying to learn. you have the following: <?php for($x=0; $x<=$items; $x++) { $id=$result[$x]['pol_id']; $thing=$result[$x]['pol_policy_number']; $options.="<OPTION VALUE=\"$id\">".$thing; } ?> where it should be: <?php for($x=0; $x<=$items; $x++) { $id=$result[$x]['pol_id']; $thing=$result[$x]['pol_policy_number']; $options.="<OPTION VALUE=\"$id\">".$thing . "</OPTION>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250767-dropdown-database-select/#findComment-1286644 Share on other sites More sharing options...
thomasw_lrd Posted November 9, 2011 Share Posted November 9, 2011 Ty Quote Link to comment https://forums.phpfreaks.com/topic/250767-dropdown-database-select/#findComment-1286647 Share on other sites More sharing options...
undertaker Posted November 9, 2011 Author Share Posted November 9, 2011 Thanks everyone It helped! SOLVED! Not trying to highjack the thread by the way, just trying to learn. No problem Quote Link to comment https://forums.phpfreaks.com/topic/250767-dropdown-database-select/#findComment-1286671 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.