php - ajax update textfield from database

146

I am new to ajax and i wanna ask a question that i tried to fix alot but couldn't figure out how. I have a html form which have 2 textfield areas. And i want to update second one from my database when user enter a value to first textfield.

I went through many online tutorials and i myself wrote something like this:

html:

<tr>
    <td width="20"></td>
    <td width="229" bgcolor="#FCECEC">ID</td>
    <td width="319" bgcolor="#FCECEC">NAME</td>  
    <td width="82" align="left"><label>
        <input type="button" name="ADD" id="ADD" value="New User" onclick=" "/>
    </label></td>
</tr>
<tr>  
    <td bgcolor="#FCECEC" align="center">1</td>
    <td><input type="text" name="ID" id="ID" maxlength="11" onchange="showUser(this.value)"   value=" " /></td>
    <td><input type="text" size="40" name="NAME" id="NAME" maxlength="80" value="" /></td>
</tr> 

ajax function:

function showUser(str)
{
    if (str=="")
    {
        document.getElementById("NAME").innerHTML="";
        return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("NAME").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","/myproject/users_update.php?q="+str,true);
    xmlhttp.send();
}

and a part of php side:

$q=$_REQUEST["q"];
$kps->WhatsId($q);
$tname=$kps->NAME;
$tsname=$kps->SURNAME;

When i do that i keep getting

undefined index q

Where did i go wrong? Is it any true? Any help would be appreciated. Thank you!

979

Answer

Solution:

it is rather hard to know exactly what the problem is from the little information you provided.

like Bryan has mentioned you should change thedocument.getElementById("NAME").innerHTML todocument.getElementById("NAME").value

but this is not what causing the problem. in my opinion you got the url here wrong:xmlhttp.open("GET","/myproject/users_update.php?q="+str,true);

but I can't tell for sure because I haven't seen you project directories

People are also looking for solutions to the problem: javascript - How to read Excel File data in php while sending it via jquery

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.