如果找不到ID,则会显示错误消息

new*_*der 0 php forms ajax

我有一个简单的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;
Run Code Online (Sandbox Code Playgroud)

这是我的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];
                    }
           } 
    }   
}
Run Code Online (Sandbox Code Playgroud)

Rez*_*aSh 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)) {
Run Code Online (Sandbox Code Playgroud)

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

4.如果要在ajax响应上显示消息,则可以使用json或....作为一个简单示例,在出现错误时返回此字符串:

error|" , , ," . $param;
Run Code Online (Sandbox Code Playgroud)

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

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

编辑:

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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)