0
votes

I have one simple html form where the fields can be auto populated by entering ID. its working fine. but, if ID not found in database, it can only return null to the form fields. i was trying to display an error message (can be a pop-up window) saying ID not found! but i failed to do it. here is my code to echo info into the form field:

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;

here is my 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
Could you also add your ajax code, it may turn out you could echo '<script>alert("ID not found");</script>'; in the else portionDale

2 Answers

5
votes
  1. Dont use mysql_* functions, use PDO or Mysqli instead.
  2. take care about $param value
  3. If your query should return 1 result, you can use LIMIT 1 , and also there is no need to use while.

change this :

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

to

$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. if you want to show a message on your ajax response, you can use json or .... as an simple example, return this string on error :

error|" , , ," . $param;

and to check if an error occured on your client :

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

Edit :

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
votes

try do this in your sql

 LIKE '%" . $param . "%'