douqiu1604 2014-05-04 09:35
浏览 43

onblur ajax从数组中获取多个表单字段值

Hi i have a very tiny problem. I am using ajax call to fetch values onblur of customer id. I am able to get one value in form but i want multiple values to fill form fields. my partial code is as follows:

req.onreadystatechange = processReqChange; 
req.open("GET", url + '?mob=' + number, true); 
req.send(null); 

function processReqChange() {

    if (req.readyState == 4) { 

        if (req.status == 200) { 
            document.getElementById('oper').value = req.responseText;
                               }
                             }
                            } 

My php code for sending the data:

$f = fopen('file.csv', "r");
    $result = false;
    while ($row = fgetcsv($f)) {
        if ($row[0] == $mob) {
            $result = $row[1];
            break;
        }
    }
    fclose($f);
    echo $result;  

How should i send multiple values like $row[2], $row[3] to fill form fields like username, email etc. Please advice.

  • 写回答

1条回答 默认 最新

  • dongniechi7825 2014-05-04 09:39
    关注

    The standard way would be to have PHP return a JSON string and parse that in JS.

    Example in PHP (nothing dynamic here):

    echo '{"key1":"value1", "key2":"value2", "key3":"value3"}';
    

    Then using that in JS:

    var result = JSON.parse(req.responseText);
    // now you can use the values returned by PHP
    alert(result.key1);
    

    For more info on how to produce JSON with PHP: http://www.php.net/manual/book.json.php

    Please note that this example is highly primitive. You might want to add more JSON sanity/validity checks in the JS code and, of course, a more elegant PHP generation.

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line