Six_Actual Posted May 27, 2019 Share Posted May 27, 2019 I am trying to load customer names into a select drop-down list. The first drop down list should result in the second drop down list being updated. However, I am struggling to achieve this, as the list remains empty (only displays Select). <select class="form-control" id="typelist" name="typelist" onchange="get_namelist()";> <option value="cases">Case</option> <option value="processors">Processor</option> </select> <div id="get_namelist"></div> <script type="text/javascript"> function get_namelist() { // Call to ajax function var typelist = $('#typelist').val(); var dataString = "typelist="+typelist; $.ajax({ type: "POST", url: "mysubselect.php", data: dataString, success: function(html) { $("#get_namelist").html(html); } }); } </script> mysubselect.php: <?php include "config.php"; if ($_POST){ $list = $_POST['typelist']; echo("<script>console.log('PHP: ".$list."');</script>"); if ($list != '') { $sql = "SELECT name FROM parts WHERE type=" . $list; $result = $link->query($sql); echo "<select class='form-control' name='partlist'>"; echo "<option value=''>Select</option>"; while($row = $result->fetch_assoc()) { echo "<option value='".$row['name']."'>".$row['name']."</option>"; } echo "</select>"; } else{ echo ""; } } ?> The second list is empty and only outputs Select. What am I doing wrong? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/308761-load-mysql-result-into-select-list/ Share on other sites More sharing options...
Barand Posted May 27, 2019 Share Posted May 27, 2019 (edited) String literals in a query need to be in quotes. try $sql = "SELECT name FROM parts WHERE type='$list' "; EDIT: You should be using a prepared statement and not putting POSTed data directly into your query. Edited May 27, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/308761-load-mysql-result-into-select-list/#findComment-1567098 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.