I've only just started playing with AJAX, as part of a project I'm dealing with at work.

The specific piece I'm stuck on at the moment is this:

I have a form with a lot of fields, two of which are subnet (a drop box populated from a database, via PHP) and IP address (a text input box). When the user selects a subnet, the value is sent to a PHP script that queries the DB and returns the next unused IP address. If all I wanted was to display that in a div next to the IP input box, I'd be set -- that works fine.

Unfortunately, I can't for the life of me figure out how to get the returned data into a text input box, which is what I actually want.

I know that document.getElementById("div_id").innerHTML=request.responseText; will display the text in a div in my sample code. I found a post somewhere that implied that frm.getElementById("input_id").value = request.responseText; would do the job for a text input, but it doesn't, at least in any of the browsers I've tested.

If anyone could point me in the right direction, I'd greatly appreciate it.

Thanks in advance,

8 Years
Discussion Span
Last Post by amckenzie4

Wouldn't you know it... after searching for days, I post here, and half an hour later I find a solution. In brief, here's how it works, assuming that the form has an id of "add_form" and the field is named "ip" and also has an ID of "ip":

document.getElementById('add_form').elements.namedItem('ip').value = request.responseText;

I'm not sure why I never got it to work with just "document.getElementById('ip').value = request.responseText", but I'll assume I either had a typo or misunderstood something.

Hopefully this will help the next person who's looking for this solution!


P.S. I found the solution at http://userscripts.org/topics/1199

Edited by Nick Evan: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.