JavaScript basic Dynamic Select list PHP & AJAX Jquery visual basic select dynamic range

I was trying to get a dynamic dependent select list using AJAX, but unable to get the second list. Here is my code. gethint.php is working fine. I don't know where I am doing wrong.

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"</script>      

        <script>
        $(document).ready(function()
        {
            $('#brand').change(function()
            {
                var cid=$('#brand').val();
                if(cid !=0)
                {

                $.ajax({
                    type:'post',
                    url: 'gethint.php',
                    data: {id:cid},
                    cache:false,
                    success: function(returndata)
                        {
                            $('#model').html(returndata);
                        }
                    });
                }
            })
        })
        </script>
    </head>
    <body>
        <header>
            <h1>Car Comparision </h1>
        </header>
        <form method="post" action="">
            Brand 1:
            <select id="brand" class="brand">
                    <?php
                    include "connect.php"; 
                    $query=$con->query("SELECT * FROM car");
                    while($brand=$query->fetch_assoc())
                    {
                    $brand_sel='<option value="'.$brand['id'].'"'.">".$brand['brand'].'</option>'."\n";
                    echo $brand_sel;
                    }
                    ?>
            </select>
            Model 1:
            <select id="model" class="model">
            <option value="0">Please select a city</option>
                <option></option>
            </select>
            <input type="submit" value="submit">
        </form>
    </body>
</html>

code for my gethint.php file

<?php
require ("connect.php");
$Query='SELECT * FROM model WHERE id='.$_POST['id'];
$sql=$con->query($Query) or die(mysql_error());
//print_r($Query);
while($row=$sql->fetch_array(MYSQLI_ASSOC)) {
    ?>
    <option value="<?php echo $row["id"];?>"><?php echo $row['model_name'];?></option>
<?php
}
?>
Answer:1

Pls try this code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

    <script>
    $(document).ready(function()
    {
       $('#brand').on('change', function() {
            var cid=$('#brand').val();
            if(cid !=0)
            {

            $.ajax({
                type:'post',
                url: 'gethint.php',
                data: {id:cid},
                cache:false,
                success: function(returndata)
                    {
                        $('#model').html(returndata);
                    }
                });
            }
        })
    })
    </script>
Answer:2

I would do it of a different a way:

<?php
require ("connect.php");
$Query='SELECT * FROM model WHERE id='.$_POST['id'];
$sql=$con->query($Query) or die(mysql_error());
//print_r($Query);

$elements = [];
while($row=$sql->fetch_array(MYSQLI_ASSOC)) {
    $item = ["id" => $row['id'], "model_name" => $row['model_name']]
    array_push($elements , $item)

}
echo $elements;

?>

I would send a associative array which contains all the items of your select. I would also modify your script to:

  <script>
    $(document).ready(function()
    {
        $('#brand').change(function()
        {
            var cid=$('#brand').val();
            if(cid !=0)
            {

            $.ajax({
                type:'post',
                url: 'gethint.php',
                data: {id:cid},
                cache:false,
                success: function(returndata)
                    {
                        returndata.each(data,function(){
                            $('#model').append("<option value="+data.id+">"+data.model_name+"</option>")
                         })

                    }
                });
            }
        })
    })
    </script>
Answer:3

I have this handler that I built to handle $http requests. It looks like this: .service('ApiHandler', ['$q', '$http', '$injector', 'apiUrl', 'ErrorService', 'toastr', function ($q, $http, $injector, ...

I have this handler that I built to handle $http requests. It looks like this: .service('ApiHandler', ['$q', '$http', '$injector', 'apiUrl', 'ErrorService', 'toastr', function ($q, $http, $injector, ...

  1. angularjs interceptor responseerror
  2. angularjs http interceptor responseerror
  3. angularjs interceptor response error handling

My first question here :) I'm porting my site to Angular (I'm new to it) and wondering if I'd be able to keep my pure CSS modal dialogue box while using ui-router. In HTML it looks like this: &...

My first question here :) I'm porting my site to Angular (I'm new to it) and wondering if I'd be able to keep my pure CSS modal dialogue box while using ui-router. In HTML it looks like this: &...

I have two arrays which I want to merge. var arrayOne = [{id:1},{id:2}]; var arrayTwo = [{age: 20}, {age: 25}, {age: 22}]; Merged array should be like this [{id:1, age: 20}, {id:2, age: 25}, {age: ...

I have two arrays which I want to merge. var arrayOne = [{id:1},{id:2}]; var arrayTwo = [{age: 20}, {age: 25}, {age: 22}]; Merged array should be like this [{id:1, age: 20}, {id:2, age: 25}, {age: ...

  1. merge array objects javascript
  2. merge array objects lodash
  3. merge array objects
  4. merge array objects php
  5. merge array objects es6
  6. concat array objects javascript
  7. concat array objects
  8. merge two array objects in javascript
  9. merge 2 array objects javascript
  10. merge array of objects by key
  11. merge array of objects js
  12. merge array of objects javascript es6
  13. merge array of objects without duplicates javascript
  14. merge array of objects typescript
  15. merge two array objects in php
  16. merge array of objects by property
  17. merge array of objects by key js
  18. merge two array objects
  19. merge array of objects angular
  20. jquery merge array objects

I am creating a self scrolling, auto updating table and need to search through through elements of a class from a certain point. My code currently is this: var nextScroll; setInterval(function() { ...

I am creating a self scrolling, auto updating table and need to search through through elements of a class from a certain point. My code currently is this: var nextScroll; setInterval(function() { ...

  1. jquery loop through array
  2. jquery loop through elements
  3. jquery loop through json
  4. jquery loop through table rows
  5. jquery loop through elements by class
  6. jquery loop through select options
  7. jquery loop through json array
  8. jquery loop through child elements
  9. jquery loop through checkboxes
  10. jquery loop through json object
  11. jquery loop through form elements
  12. jquery loop through array of objects
  13. jquery loop through list
  14. jquery loop through dictionary
  15. jquery loop through table
  16. jquery loop through checked checkboxes
  17. jquery loop through inputs
  18. jquery loop through elements by name
  19. jquery loop through td in tr
  20. jquery loop through divs