Hi guys,

I need to put data that i get it from db2 and display it into each text boxes (Name, Address and Postcode). What i got now, its displayed Name's data that i got from db2 into all text boxes (Name, Address and Postcode) . All i want is each data for each text box.
Here is my code :

//html
<tr>
                <td height="25" align="right">Id Num <font color="#FF0000">*</font></td>
                <td height="25" align="center">:</td>
                <td height="25"><input name="id_num" type="text" id="id_num" size="12"  maxlength="12" onKeyUp="checkInput(this.value, 'ajaxChecker-name.php');" value="<?php echo $selected_id_Num; ?>"/>
                </td>
            </tr>     

            <tr>
                <td height="25" align="right">Name </td>
                <td height="25" align="center">:</td>
                <td height="25"><input type="text" name="name" id="name" size="30" maxlength="50"></td>
            </tr>     

            <tr>
                <td height="25" align="right">Address </td>
                <td height="25" align="center">:</td>
                <td height="25"><input type="text" name="address" id="address" size="30" maxlength="50" ></td>
            </tr>               

            <tr>
                <td height="25" align="right">Postcode </td>
                <td height="25" align="center">:</td>
                <td height="25"><input type="text" name="postcode" id="postcode" size="30" maxlength="50"></td>
            </tr>   


    //checkInput.js
    var xmlhttp

    function checkInput(str, checker_file){

        xmlhttp=GetXmlHttpObject();
        if (xmlhttp==null){                
          alert ("Your browser does not support AJAX!");                
        return;
        }

        var url=checker_file;
        url=url+"?query="+str;
        url=url+"&sid="+Math.random();
        xmlhttp.onreadystatechange=stateChanged;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);

         function stateChanged(){
             if (xmlhttp.readyState==4){                 
             document.getElementById("name").value=xmlhttp.responseText;  
             document.getElementById("address").value=xmlhttp.responseText;      
             document.getElementById("postcode").value=xmlhttp.responseText;
             }
         }
    }

    function GetXmlHttpObject(){

        if (window.XMLHttpRequest){
          // code for IE7+, Firefox, Chrome, Opera, Safari
          return new XMLHttpRequest();
        }

        if (window.ActiveXObject){
          // code for IE6, IE5
          return new ActiveXObject("Microsoft.XMLHTTP");
        }

    return null;

}

//ajaxChecker-name.php
    include('include/connect_db.php');

    $id_num = $_REQUEST["query"];

    $host       = "xx";
    $user       = "xx";
    $pass       = "xx";

    $db2connect = db2_connect($host, $user, $pass);

                $check_Id_Num = "
                                    SELECT 
                                           COUNT(ID_NUM) AS HIT
                                    FROM 
                                           DB_NAME 
                                    WHERE 
                                           ID_NUM = '$id_num'
                         ";
                $result_Id_Num = db2_exec($db2connect, $check_Id_Num);
                $row_result = db2_fetch_array($result_Id_Num);

                $hit_Id_Num = $row_result['0'];

                if($hit_Id_Num == true){
                    $checkIdNum = "SELECT 
                                           NAME,
                                           ADD,
                                           POSTCODE
                                    FROM 
                                           DB_NAME   
                                    WHERE 
                                           ID_NUM = '$id_num'";
                    $resultIdNum = db2_exec($db2connect, $checkIdNum);
                    $row_result = db2_fetch_array($resultIdNum);
                    echo $name = $row_result['0'];
                    echo $add = $row_result['1'];
                    echo $postcode = $row_result['2'];

                }else
                {
                    echo "Id Num Not Valid";
                }

Any suggestion guys?

Recommended Answers

All 2 Replies

First of all, the php code(line 106 - 108)
These codes show that the return of the php code will display $name,$add and $postcode with each of the value append behind.

Next I seen your ajax response(line 47 - 50)
You assign the same data into each of the field which lead to the problem.

Just a suggestion here based on what I will do:

  1. php code: put the $name,$add and $postcode in a array and echo the result in array or json format.

    $result['name'] = $row_result['0'];
    $result['address'] = $row_result['1'];
    $result['postcode'] = $row_result['2'];

  2. ajax part: convert the data received into readable data based on json/array returned.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.