dongzao3923 2013-11-19 10:57
浏览 275
已采纳

通过切换选择中的选项,在不同的表列中显示不同的数据

My problem is the following:

I have an ajax function that, according to the option (of a select) selected, associate a record in a database and populate another input, i.e. a p tag.

I have two td tags that have to be populated. Different data has to be displayed, so i want that, according to the input on the first select, on the second td there will be input y, in the third input z and so on... how can it be possible? If i try to append data to more than one tag, the same data is displayed in all the td columns.

Here i attach my code

Main.php

$(document).ready(function() {  
    $('#L_NAME0').change(function() {
        var L_NAME0 = $("#L_NAME0").val();
        $.ajax({
        type: "POST",
        url: "elaborazione_dati.php",
        data: "L_NAME0=" + L_NAME0,
        dataType: "html",
        success: function(msg) {
            $("#L_AMT0").html(msg);
            $("#L_DESSERV").html(msg); 
        },
        error: function() {
            alert("Call failed");
        }
    });
 });    
});

Form.php

                                     <label for="L_DESSERV">Descrizione del servizio</label>
<p class="L_DESSERV" id="L_DESSERV"></p>
        </td

        <td class="h4">  
                                     <label for="L_AMT0">Costo del servizio</label>
<p class="L_AMT0" id="L_AMT0"></p>                
        </td>

elaborazione_dati.php

$tipologia_selezionata = $_POST['L_NAME0'];

$sql = "SELECT * FROM acquisti WHERE durata = '$tipologia_selezionata' ";
$q = $db->prepare($sql);
$q->execute();

$q->setFetchMode(PDO::FETCH_ASSOC);

while($caratt = $q->fetch()) {
    echo '<input readonly="readonly" type="hidden" name="L_NAME0" value="'.$caratt['durata'].'"/>';
    echo '<input readonly="readonly" type="hidden" name="L_AMT0" value="'.$caratt['prezzi'].'"/>';
    echo $caratt['prezzi']; ?> &euro; <?php
} 

Any suggestions?

Thanks a lot!

  • 写回答

1条回答 默认 最新

  • dqfkd82886 2013-11-19 11:54
    关注

    You need to split the results and the easiest way is to return JSON from PHP and then process it on your js code to generate the fields and text. So in PHP something like:

    while($caratt = $q->fetch()) {
        $result->durata = $caratt[duratta];
        $result->prezzi = $caratt[prezzi];
    }
    echo json_encode($result); 
    

    then in your js something like:

       $('#L_NAME0').change(function() {
            var L_NAME0 = $("#L_NAME0").val();
            $.ajax({
            type: "POST",
            url: "elaborazione_dati.php",
            data: "L_NAME0=" + L_NAME0,
            dataType: "json",
            success: function(data) {
    
                $("#L_AMT0").html("<input type='hidden' name='L_NAME0' value='"+data.duratta+"'/>"+data.duratta);
                $("#L_DESSERV").html("<input type='hidden' name='L_DESSERV' value='"+data.prezzi+"'/>"+data.prezzi+"&euro;"); 
            },
            error: function() {
                alert("Call failed");
            }
        });
    

    However it seems confusing that you put another input named L_NAME0 - the id of your select control, but hey, it's your code... :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算