简体   繁体   中英

JavaScript onchange not working on dropdown element

I am creating an application that will search db and later on allow user to compare results. For that i need multiple dependable drop down menus. I am not expert coder, have some knowledge of HTML and PHP. For this i have to use Javascript.

Now if i try to use same script with diferent information that will create dropdown3, it does not work.

Here is code of index page:

<?php
require('assets/classes/manufacturer.php');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link href="style/style.css" rel="stylesheet" type="text/css">
<title>Untitled Document</title>
</head>
<body>
<div id="wrapper">
    <div id="sidebar">
        <div id="dropdown1">
                <select name="proizvodac" id="proizvodac-select">
                    <option value="">Odaberi Proizvođača</option>
                    <?php foreach($proizvodaci as $proizvodac): ?>
                        <option value="<?php echo $proizvodac['id']; ?>"><?php echo $proizvodac['proizvodac']; ?></option>
                    <?php endforeach; ?>
                </select>
        </div>
        <div id="dropdown2">
        </div> 

        <div id="dropdown3">
        </div>   
    </div>
    <div id="grupe">
        <div id="grupa1">
        </div>
        <div id="grupa2">
        </div>
        <div id="grupa3">
        </div>
    </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="js/global.js"></script>
</body>
</html>

And script is here:

$('#proizvodac-select').on('change', function() {
    var self = $(this);
    $.ajax({
        url: 'http://localhost/assets/classes/model.php',
        data: { proizvodac: self.val()},
        success: function(data){
                $('#dropdown2').html(data);
        }
    });
});

This should call model.php when selection is made in first dropdown, first dropdown is created from manufacturer.php that is required on start.

model.php:

<?php
require('database.php');
if(isset($_GET['proizvodac'])) {
    $modelQuery = "SELECT 
                        * 
                    FROM auti 
                    WHERE proizvodac_id = :proizvodac_id
    ";
    $modeli = $db->prepare($modelQuery);

    $modeli->execute(['proizvodac_id' => $_GET['proizvodac']]);

    $selectedModel = $modeli->fetch(PDO::FETCH_ASSOC);
}
?>
         <select name="model" id="model-select">
            <option value="">Odaberi Model</option>
            <?php foreach($modeli as $model): ?>
                <option value="<?php echo $model['id']; ?>"><?php echo    $model['model']; ?></option>
            <?php endforeach; ?>
        </select>

NOTE: I have probably did something wrong when i was trying to create third dropdown that depends on second one.

EDIT: I have did something wrong, thanks kingkero for finding it.

So what i need now, if you can tell me how to get third dropdown menu and so on. I need like 10 of them. All should depend on previous one.
And if you notice some bad practice in my code, please let me know.

Now i got script for third dropdown and it is working if run from console in chrome but can't get it to work by itself.

SCRIPT HERE:

$('#model-select').on('change', function() {
    var self = $(this);
    $.ajax({
        url: 'http://localhost/assets/classes/opcija_modela.php',
        data: { model: self.val()},
        success: function(data){
                $('#dropdown3').html(data);
        }
    });
});

Thank you in advance

Your Script file should be include with in the head tag

Ex:

 <?php require('assets/classes/manufacturer.php'); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <link href="style/style.css" rel="stylesheet" type="text/css"> <title>Untitled Document</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="js/global.js"></script> </head> <body> <div id="wrapper"> <div id="sidebar"> <div id="dropdown1"> <select name="proizvodac" id="proizvodac-select"> <option value="">Odaberi Proizvođača</option> <?php foreach($proizvodaci as $proizvodac): ?> <option value="<?php echo $proizvodac['id']; ?>"><?php echo $proizvodac['proizvodac']; ?></option> <?php endforeach; ?> </select> </div> <div id="dropdown2"> </div> <div id="dropdown3"> </div> </div> <div id="grupe"> <div id="grupa1"> </div> <div id="grupa2"> </div> <div id="grupa3"> </div> </div> </div> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM