开发者

AJAX, refresh INPUT field

开发者 https://www.devze.com 2023-01-29 11:30 出处:网络
I am using AJAX for the first time with PHP and mySQL. I have been following some examples and tutorials on how to use AJAX with INPUT fields.

I am using AJAX for the first time with PHP and mySQL. I have been following some examples and tutorials on how to use AJAX with INPUT fields.

I have a form with 2 input fields.

  • 1st input field: User type in 4 digits
  • 2nd input field: The 4 digits are looked up in the mySQL db and outputted on this field.

Most examples are based on output in div's and span's while i am interested in output on my 2nd INPUT field.

How is this possible?

My js-file:

function showUser(str)
{
  if (str=="")
  {
    document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseTex开发者_运维问答t;
    }
  }
  xmlhttp.open("GET","include/postalcode.php?q="+str,true);
  xmlhttp.send();
}


You have written :

document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

Instead of innetHTML use value:

document.getElementById("txtHint").value=xmlhttp.responseText;


Most examples are based on output in div's and span's while i am interested in output on my 2nd INPUT field.

You should be able to follow the examples with two changes:

  • use .value instead of .innerHTML for input fields
  • look up your second input field by its ID

document.getElementById("secondFieldId").value=xmlhttp.responseText;

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号