普通网友 2015-05-20 10:26
浏览 34

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>';


}

?>
  • 写回答

1条回答 默认 最新

  • weixin_33734785 2015-05-20 12:48
    关注

    If you giving

    dataType: 'json'

    ajax return type should be json format else it wont work.

    remove that line or use

    dataType: 'html' ->if you expecting html 
    dataType: 'text' ->if you expecting plain text

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀