0

ID を入力することでフィールドに自動入力できる単純な html フォームが 1 つあります。その正常に動作します。ただし、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_* 関数を使用しないでください。代わりにPDOまたはMysqliを使用してください。
  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 に答える