ScarfaceJM Posted March 17, 2015 Share Posted March 17, 2015 What's up guys! New to the forum sorry my first post is asking for help, but here's what I got. I have a database setup as follows: id = 1 Name = sample Level = 99 Status = sampleStatus Updated = dateLastUpdated I have a page setup that gets users from the database and puts them into a drop down list. Just the names not the enter info. <?php require 'connect.php'; $database = 'member_list'; mysql_select_db($database) or die("Could not connect to database: ".mysql_error()); $query = "SELECT id, Name FROM members ORDER BY Name ASC"; $query_result = mysql_query($query) or die("Query failed".mysql_error()); while($row = mysql_fetch_array($query_result)) { $id = $row['id']; $name = $row['Name']; echo "<option value=\"$id\"> $name </option>"; } ?> Now what I would like to do, is have it setup where when I click on a name from this menu, it will populate a table with all the users information. So if I click on Todd his info will populate a table. Then when I click on another name in the list, Todds info will erase and Tom's info will now populate the table. I don't know if it can be done with php as I'm fairly new and still learning. Quote Link to comment https://forums.phpfreaks.com/topic/295302-drop-down-menu/ Share on other sites More sharing options...
iarp Posted March 17, 2015 Share Posted March 17, 2015 (edited) To strictly use PHP you could wrap the <select> in a <form>, add a Submit button and then use the value of the <option> selected to run your SELECT statement against. <form method="post"> <select name="username"> <option value="ScarfaceJM">ScarfaceJM</option> <option value="iarp">iarp</option> </select> <input type="submit" name="submit" value="Submit" /> </form> The selected username would then be available only after submission by: $username = $_GET['username']; The other way would be via jQuery or javascript in some form. That would allow you to stay on the page and load data in the background. <script type="text/javascript"> $(document).ready(function(){ $('select[name="username"]').on('click', function() { // do something ajaxy like and load it from another source maybe? console.log('i am in here'); }); }); </script> Edited March 17, 2015 by iarp Quote Link to comment https://forums.phpfreaks.com/topic/295302-drop-down-menu/#findComment-1508273 Share on other sites More sharing options...
ginerjm Posted March 17, 2015 Share Posted March 17, 2015 iarp gave you some good examples of how to do this but did include a small error in his code. The form he coded up for you uses the POST method. Notice? Then in the ensuing PHP code he showed you how to capture the contents of the dropdown (from the name attribute of the select tag) by grabbing the element from the GET superglobal. Problem is the data will not be in GET since your form is doing a POST. Swap out his use of $_GET for $_POST. 1 Quote Link to comment https://forums.phpfreaks.com/topic/295302-drop-down-menu/#findComment-1508308 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.