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

时间:2012-12-14 09:55:55

标签: php ajax html-form

我有一个简单的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];
                    }
           } 
    }   
}

2 个答案:

答案 0 :(得分:4)

  1. 不要使用mysql_ *函数,而是使用PDOMysqli
  2. take care关于$ param value
  3. 如果您的查询应返回1个结果,则可以使用LIMIT 1,也无需使用。
  4. 改变这个:

    $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];
            }
        }
    }
    

答案 1 :(得分:1)

尝试在你的SQL中执行此操作

 LIKE '%" . $param . "%'