php - ajax update textfield from database


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:


    <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=" "/>
    <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>

ajax function:

function showUser(str)
    if (str=="")
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp.readyState==4 && xmlhttp.status==200)

and a part of php side:


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!




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"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


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.