dourao1968 2015-07-19 01:07
浏览 110
已采纳

使用ajax从文本框中的php变量中获取特定值

PHP CODE(plan-api.php)

$requestUrl = "url.com";
$response = (file_get_contents($requestUrl, false, $context));
$data = (json_decode($response, true));

echo $data["data"][0][recharge_amount];
echo "<br>";
echo $data["data"][0][recharge_talktime];
echo "<br>";
echo $data["data"][1][recharge_amount];
echo "<br>";
echo $data["data"][1][recharge_talktime];
echo "<br>";
echo $data["data"][2][recharge_amount];
echo "<br>";
echo $data["data"][2][recharge_talktime];
echo "<br>";

HTML/AJAX CODE(plan-ajax.php)

Please enter a Mobile number
<input type="text" id="search">
<br>
<input type="text" id="result">
<input type="text" id="result1">
<input type="text" id="result2">
<input type="text" id="result3">
<script>
$(document).ready(function() {
    $('#search').keypress(function(){
        $.ajax({

            type: "GET",
            url: "plan-api.php",
            data: 'result=' + $('#search').val(),

            success: function(output){
            $('#result').val(output);

        }


    }); // Ajax Call



}); //event handler


}); //document.ready
</script>

OUTPUT(all in textbox having id "result")

10
7.77
20
15.54
30
23.32

but i want to get output :
echo $data["data"][0][recharge_amount] in textbox id "result"
echo $data["data"][1][recharge_amount] in textbox id "result1"
echo $data["data"][2][recharge_amount] in textbox id "result2"

and so on....

  • 写回答

1条回答 默认 最新

  • dongmo9996 2015-07-19 01:44
    关注

    I hope I have not mistyped anything.

    PHP:

    <?php
    
    /*
     * We assume your data has this structure, for example:
     * 
    $data['data'][0]['recharge_amount'] = 10;
    $data['data'][0]['recharge_talktime'] = 5;
    $data['data'][1]['recharge_amount'] = 11;
    and so on
    */
    
    // we output the data as JSON
    echo json_encode($data);
    
    ?>
    

    HTML / JS

    Please enter a Mobile number
    <input type="text" id="search">
    <br>
    <input type="text" id="result0">
    <input type="text" id="result1">
    <input type="text" id="result2">
    <input type="text" id="result3">
    <script>
    $(document).ready(function() {
        $('#search').keypress(function(){
            $.ajax({
    
                type: "GET",
                url: "plan-api.php",
                data: 'result=' + $('#search').val(),
                dataType: "json",
    
                success: function(responseText){
                    $.each(responseText.data, function(key,value){
                        $('#result'+key).val(value.recharge_amount);
                    });
                }
    
            }); // Ajax Call
        }); //event handler
    
    }); //document.ready
    </script>
    

    btw. I have changed id="result" to id="result0" for ease.

    EDIT: actually a part of my php was redundant, so I changed it.

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?