使用AJAX从php文件中获取多个数据,并将它们插入到不同的输入字段中

时间:2014-02-06 08:06:19

标签: javascript php jquery ajax

我正在使用AJAX来访问php文件中的数据。 我从数据库中检索数据的格式有问题,请帮忙。

所以,这是我的ajax函数拼接。它从find_account.php

中检索数据
function processReqChange() { 
    // only if req shows "loaded" 
    if (req.readyState == 4) { 
        // only if "OK" 
        if (req.status == 200) { 
            form_prof.prof_id.value = req.responseText;
    form_prof.prof_name.value = req.responseText;
    form_prof.prof_username.value = req.responseText;
    form_prof.prof_password.value = req.responseText; 
        } 
        else { 
            alert("Problem in retrieving the XML data:\n" + req.statusText); 
        } 
    } 
}

find_account.php

<?php

include("connect.php");
session_start();

$account = $_GET['account'];
$query = "SELECT * FROM profs WHERE profs_name = '".$account."'";
$result = mysql_query($query);
$num = mysql_num_rows($result);

if(empty($num))
{
echo 'DATA NOT FOUND';
}
else
{
    $arr = mysql_fetch_array($result);
    $id = $arr['profs_number'];
$name = $arr['profs_name'];
$username = $arr['profs_username'];
$password = $arr['profs_password'];
}

header("Content-type: text/plain");
echo $id;
echo $name;
echo $username;
echo $password;
?>

我的HTML中有4个输入框,其中req.responseText放置值

每当我在输入字段中搜索名称时,例如:

Search: [ Dorothy Perkins ]

输出类似于[id,name,username,password]:

[20111Dorothy Perkinsdperkins@mail.com123456] [same with 1st field] [same] [same] 

我希望它像... ...

[20111]  [Dorothy Pekins]  [dperkins@mail.com]  [123456]

其中[]是输入字段。 请帮我安排一下我的格式,我很困惑。我是新手。

3 个答案:

答案 0 :(得分:1)

您必须从PHP代码(XML,json,或简单地用逗号分隔值)以某种格式编写数据,并从您的JavaScript中解析它。

例如,在PHP中:

echo $id . "," . $name . "," . $username . "," . $password;

然后在javascript中:

values = req.responseText.split(",");
form_prof.prof_id.value = values[0]
form_prof.prof_name.value = values[1];
form_prof.prof_username.value = values[2];
form_prof.prof_password.value = values[3];

当然,如果值可能包含逗号,则可能需要执行更复杂的操作。

答案 1 :(得分:1)

您可以在发回之前对json中的返回值进行编码。 在PHP中

<?php

include("connect.php");
session_start();

$account = $_GET['account'];
$query = "SELECT * FROM profs WHERE profs_name = '".$account."'";
$result = mysql_query($query);
$num = mysql_num_rows($result);

if(empty($num))
{
$returnValues = 'DATA NOT FOUND';
}
else
{
    $arr = mysql_fetch_array($result);
    $returnValues = json_encode($arr);
}

echo $returnValues;
?>

在Javascript中

function processReqChange() { 
    // only if req shows "loaded" 
    if (req.readyState == 4) { 
        // only if "OK" 
        if (req.status == 200) { 
           req = JSON.parse(reg);

            form_prof.prof_id.value = req.id;
    form_prof.prof_name.value = req.name;
    form_prof.prof_username.value = req.username;
    form_prof.prof_password.value = req.password; 
        } 
        else { 
            alert("Problem in retrieving the XML data:\n" + req.statusText); 
        } 
    } 
}

答案 2 :(得分:0)

你可以试试这个

$account = $_GET['account'];
$query = "SELECT * FROM profs WHERE profs_name = '".$account."'";
$result = mysql_query($query, MYSQLI_STORE_RESULT);
while($arr = $result->fetch_array(MYSQLI_ASSOC)) {
    $returnValues = json_encode($arr);
    break;
}

echo $returnValues;

请注意,列名称用作$ arr

的关联索引

希望它有效。