A JM Posted July 29, 2009 Share Posted July 29, 2009 How do I populate and refresh a second listbox based upon the choice a user makes in the first listbox? Currently this is what I'm using for my first listbox but I'm not sure how to complete the second listbox can someone help me out with this? <?php //Get list of Clients from database $query_rstClients = "SELECT DISTINCT clnt_name FROM orders ORDER BY clnt_name DESC"; mysql_select_db($database_dbconn, $dbconn); $rstClients = mysql_query($query_rstClients, $dbconn) or die(mysql_error()); $selectBox = "<select id='clientname' name = 'clientname' onchange='somefunction();'><option>Select Client</option>"; while($rec=mysql_fetch_array($rstClients)) { $selectBox = $selectBox."<option value= $rec[clnt_name]>$rec[clnt_name]</option>"; } $selectBox = $selectBox."</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/167948-solved-populate-one-textbox-based-upon-another/ Share on other sites More sharing options...
patrickmvi Posted July 29, 2009 Share Posted July 29, 2009 This would seem to me more of a JS/Ajax type issue unless you're planning on having your page refresh when something is picked from the first select box. Please specify how you want this to behave. Quote Link to comment https://forums.phpfreaks.com/topic/167948-solved-populate-one-textbox-based-upon-another/#findComment-885802 Share on other sites More sharing options...
ignace Posted July 29, 2009 Share Posted July 29, 2009 if (!empty($_POST)) {//something was posted if (!empty($_POST['first_select']) && empty($_POST['second_select'])) {//nothing selected in $query = 'SELECT * FROM table WHERE parent_id = ' . $_POST['first_select'];//don't use it like this just an example $result = mysql_query($query, $db); .. } else if (!empty($_POST['first_select']) && !empty($_POST['second_select'])) { $first_select = $_POST['first_select']; $second_select = $_POST['second_select']; } } Quote Link to comment https://forums.phpfreaks.com/topic/167948-solved-populate-one-textbox-based-upon-another/#findComment-885805 Share on other sites More sharing options...
A JM Posted July 29, 2009 Author Share Posted July 29, 2009 This would seem to me more of a JS/Ajax type issue unless you're planning on having your page refresh when something is picked from the first select box. Please specify how you want this to behave. Ultimately I'm planning on using the selected items from both select boxes in a query for the current page so I'm open to suggestions. Ignace, does that mean that after the user selects the first item that the page needs to post? I'm still a bit fuzzy about how to correctly use $_POST to refresh the current page. Not sure I'm reading the script correctly: If $_POST not empty then test for 'first_select' not empty and 'second_select' is empty run query to populate second select if true else if 'first_select' not empty and 'second_select' not empty set variables. Could I then run my query for the page after both are filed? How do I refresh the page? ... $first_select = $_POST['first_select']; $second_select = $_POST['second_select']; $query = 'SELECT * FROM table WHERE parent_id = ' . $_POST['first_select'] AND parent_name = ' . $_POST['select_select'];//don't use it like this just an example $result = mysql_query($query, $db); ???refresh page??? Quote Link to comment https://forums.phpfreaks.com/topic/167948-solved-populate-one-textbox-based-upon-another/#findComment-885955 Share on other sites More sharing options...
ignace Posted July 29, 2009 Share Posted July 29, 2009 ... $first_select = $_POST['first_select']; $second_select = $_POST['second_select']; $query = 'SELECT * FROM table WHERE parent_id = ' . $_POST['first_select'] AND parent_name = ' . $_POST['select_select'];//don't use it like this just an example $result = mysql_query($query, $db); ???refresh page??? No not refresh, fill. $secondSelectData = array(); if (!empty($_POST)) {//something was posted // assuming first field is filled and second field is awaiting data if (!empty($_POST['first_select']) && empty($_POST['second_select'])) {//nothing selected in $query = 'SELECT id, name FROM table WHERE parent_id = ' . $_POST['first_select'];//don't use it like this just an example $result = mysql_query($query, $db); .. while (list($id, $name) = mysql_fetch_array($result, MYSQL_NUM)) { $secondSelectData[$id] = $name; } } else if (!empty($_POST['first_select']) && !empty($_POST['second_select'])) { $first_select = $_POST['first_select']; $second_select = $_POST['second_select']; } } Quote Link to comment https://forums.phpfreaks.com/topic/167948-solved-populate-one-textbox-based-upon-another/#findComment-886158 Share on other sites More sharing options...
A JM Posted July 29, 2009 Author Share Posted July 29, 2009 I'm sorry I don't understand what you mean? No not refresh, fill. Quote Link to comment https://forums.phpfreaks.com/topic/167948-solved-populate-one-textbox-based-upon-another/#findComment-886219 Share on other sites More sharing options...
ignace Posted July 30, 2009 Share Posted July 30, 2009 I'm sorry I don't understand what you mean? No not refresh, fill. You don't need to refresh the page, just fill the second select with the value from the first select once the first select contains a value. $firstSelectData = array(); $query = 'SELECT id, name FROM categories'; $result = mysql_query($query, $db/*from mysql_connect()*/); while (list($id, $name) = mysql_fetch_array($result, MYSQL_NUM)) { $firstSelectData[$id] = $name; } $secondSelectData = array(); if (!empty($_POST)) {//user selected something from the first select if (!empty($_POST['first_select']) && /*make sure something was selected*/ empty($_POST['second_select'])/*second is empty, fill it!*/) { $query = 'SELECT id, name FROM subcategories WHERE category_id = ' . $_POST['first_select']; $result = mysql_query($query, $db); while (list($id, $name) = mysql_query($result, MYSQL_NUM)) { $secondSelectData[$id] = $name; } } else if (!empty($_POST['first_select']) && /*something was selected from the first select*/ !empty($_POST['second_select'])/*and the second select now also contains data*/) { $first_select_id = $_POST['first_select']; $second_select_id = $_POST['second_select']; } } Quote Link to comment https://forums.phpfreaks.com/topic/167948-solved-populate-one-textbox-based-upon-another/#findComment-886719 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.