PHP和AJAX填充选择列表

I have question about populating select box. I have first select box id='select_proizvodjaci' which work correctly, and on every change I've got correct data from function ($modelAuto = $proizvodjac->pretragaModelaPoProizvodjacu($pretraga); in firebug). But I don't know how can I populate select box select id='searchModel' in proizvodjac.php?

Here is my code:

proizvodjac.php

<form method='post' action='proizvodjac-select.php' id='proizvodjacForma'>
        <select name='pretraga' id='select_proizvodjaci'>
            <option value=''>Izaberi</option>
            <?php foreach($proizvodjaci as $sets): ?>
                    <option value='<?php echo $sets['id']; ?>'><?php echo $sets['naziv_proizvodjaca']; ?></option>
            <?php endforeach; ?>
        </select>
    </form>

<label>Model:</label><br/><br/>
    <select id='searchModel'>
        <?php foreach($modelAuta as $jedanModel): ?>
        <option value='<?php echo $jedanModel['id']; ?>'><?php echo $jedanModel['naziv_modela']; ?></option>
    <?php endforeach; ?>
    </select>

javascript.js

$('#select_proizvodjaci').change(function(){

    $.ajax({
        url: 'proizvodjac-select.php',
        dataType: 'json',
        type: 'post',
        data: 'pretraga=' + $('#select_proizvodjaci').val(),
        success: function(data) {
            $('#searchModel').html(data);
        }           
    });

});

proizvodjac-select.php

<?php 

include 'ProizvodjacModel.php';

// 1. Create database connection
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'radni_nalog_oop2';
$connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

// Test if connection occurred.
if (mysqli_connect_errno()) {
    die('Database connection failed: ' .
        mysqli_connect_error() . 
        ' (' . mysqli_connect_errno() . ')'
    );
} else {
    echo 'Connection is successfully!' . '<br/>';
}

$proizvodjac = new ProizvodjacModel($connection);


$proizvodjaci = $proizvodjac->dajProizvodjace();

if(isset($_POST['pretraga'])) {

    $pretraga = $_POST['pretraga'];

    $modelAuta = $proizvodjac->pretragaModelaPoProizvodjacu($pretraga);


    print_r($modelAuta);

    $model_option = '<option>---</option>';


}

?>
查看全部
weixin_33729196
weixin_33729196
2015/05/20 10:26
  • php
  • ajax
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信

1个回复