我对填充选择框有疑问。
我首先选择正常工作的id='select_proizvodjaci'
框,并且在每次更改时我都从函数中获得了正确的数据(在firebug中$modelAuto = $proizvodjac->pretragaModelaPoProizvodjacu($pretraga);
)。但我不知道如何在proizvodjac.php中填充选择框select id='searchModel'
?
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>';
}
?>
答案 0 :(得分:0)
如果你给予
dataType:'json'
ajax返回类型应该是json格式,否则它不会工作。
删除该行或使用
dataType: 'html' ->if you expecting html
dataType: 'text' ->if you expecting plain text