Jump to content

Load mySQL result into select list


Six_Actual

Recommended Posts

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.