0

我有一个简单的 html 表单,可以通过输入 ID 自动填充字段。它工作正常。但是,如果在数据库中没有找到 ID,它只能返回 null 到表单字段。我试图显示一条错误消息(可以是一个弹出窗口),说找不到 ID!但我没能做到。这是我将信息回显到表单字段的代码:

if (strlen($param) > 0) {
    $result = mysql_query("SELECT * FROM contact 
     WHERE contactid LIKE '$param%'");
    if (mysql_num_rows($result) == 1) {
        while ($myrow = mysql_fetch_array($result)) {
            $agentname = $myrow["contactfullname"];
            $agenttel = $myrow["contacttel"];
            $agentsal = $myrow["contactsalutation"];
            $agentid = $myrow["contactid"];
            $textout .= $agentid . ", " . $agentname . ", " . $agenttel . ", " . $agentsal;
        }
    } else {
        $textout = " , , ," . $param;
    }
}
echo $textout;

这是我的ajaxFunction:

function ajaxFunction(e){
    var e=e || window.event;
    var keycode=e.which || e.keyCode;
    if(keycode==13 || (e.target||e.srcElement).value==''){ 
    var http;  // The variable that makes Ajax possible! 

    try{ 
        // Opera 8.0+, Firefox, Safari 
        http = new XMLHttpRequest(); 
    } catch (e){ 
        // Internet Explorer Browsers 
        try{ 
            http = new ActiveXObject("Msxml2.XMLHTTP"); 
        } catch (e) { 
            try{ 
                http = new ActiveXObject("Microsoft.XMLHTTP"); 
            } catch (e){ 
                // Something went wrong 
                alert("Your browser broke!"); 
                return false; 
            } 
        }
    }
 var url = "getagentids.php?param=";
                var idValue = document.getElementById("agid").value;
                var myRandom = parseInt(Math.random()*99999999);  // cache buster
                http.open("GET", "getagentids.php?param=" + escape(idValue) + "&rand=" + myRandom, true);
                http.onreadystatechange = handleHttpResponse;
                http.send(null);

                function handleHttpResponse() {
                    if (http.readyState == 4) {
                        results = http.responseText.split(",");
                        document.getElementById('agfn').value = results[0];
                        document.getElementById('agsal').value = results[1];
                        document.getElementById('agtel').value = results[2];
                        document.getElementById('agid').value = results[3];
                    }
           } 
    }   
}
4

2 回答 2

5
  1. 不要使用 mysql_* 函数,而是使用PDOMysqli
  2. 注意$param 值
  3. 如果您的查询应该返回 1 个结果,您可以使用LIMIT 1 ,也不需要使用 while。

改变这个:

$result = mysql_query("SELECT * FROM contact 
     WHERE contactid LIKE '$param%'");
if (mysql_num_rows($result) == 1) {
        while ($myrow = mysql_fetch_array($result)) {

$result = mysql_query("SELECT * FROM contact 
     WHERE contactid LIKE '$escaped_param%' LIMIT 1");
if (mysql_num_rows($result) == 1) {
     $myrow = mysql_fetch_array($result);

4. 如果你想在你的 ajax 响应中显示一条消息,你可以使用 json 或 .... 作为一个简单的例子,在错误时返回这个字符串:

error|" , , ," . $param;

并检查您的客户端是否发生错误:

var result = "error|anything";
if(result.substr(0,6) == 'error|')
{
    alert('An error occured.');
}
else
{
    //do what you need!
}

编辑 :

function handleHttpResponse() 
{
    if (http.readyState == 4) 
    {
        results = http.responseText;
        if(results.substr(0,6) == 'error|')
        {
            alert('An error occured.');
        }
        else
        {
            results = results.split(",");
            document.getElementById('agfn').value = results[0];
            document.getElementById('agsal').value = results[1];
            document.getElementById('agtel').value = results[2];
            document.getElementById('agid').value = results[3];
        }
    }
}
于 2012-12-14T10:13:36.987 回答
1

尝试在您的 sql 中执行此操作

 LIKE '%" . $param . "%'
于 2012-12-14T10:11:51.933 回答