【问题标题】:ajax update textfield from databaseajax从数据库更新文本字段
【发布时间】:2013-02-19 18:46:21
【问题描述】:

我是 ajax 新手,我想问一个问题,我试图解决很多但不知道如何解决。我有一个 html 表单,它有 2 个文本字段区域。当用户向第一个文本字段输入值时,我想从我的数据库中更新第二个。

我浏览了许多在线教程,我自己写了这样的东西:

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 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();
}

和php端的一部分:

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

当我这样做时,我会不断得到

未定义的索引 q

我哪里做错了?这是真的吗?任何帮助,将不胜感激。谢谢!

【问题讨论】:

  • 你的函数名有错别字,showUser # Showuser
  • 你试过print_r($_REQUEST)看看你收到的参数是什么?
  • 是的,我无法将 q 作为参数。它返回空数组。但我不明白为什么。你能帮我解决这个问题吗?谢谢!
  • 你能在第一行的 str 上添加一个警报吗?
  • 尝试将 document.getElementById("NAME").innerHTML 更改为 document.getElementById("NAME").value。

标签: php html ajax


【解决方案1】:

根据您提供的少量信息,很难确切地知道问题出在哪里。

就像 Bryan 提到的那样,您应该将 document.getElementById("NAME").innerHTML 更改为 document.getElementById("NAME").value

但这不是导致问题的原因。 在我看来,你在这里弄错了网址: xmlhttp.open("GET","/myproject/users_update.php?q="+str,true);

但我无法确定,因为我没有看到你的项目目录

【讨论】:

    猜你喜欢
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 2011-01-02
    相关资源
    最近更新 更多