javascript/html/php: Is it possible to insert a value into a form with Ajax?

Posted by user1260310 on Stack Overflow See other posts from Stack Overflow or by user1260310
Published on 2012-09-22T15:11:37Z Indexed on 2012/09/22 15:37 UTC
Read the original article Hit count: 125

Filed under:
|

I have a form where users enter some text manually. Then I'd like to let the users choose a tag from a database through AJAX, not unlike how tag suggestions appear in the SO question form. While the div where the ajax call places the tag is inside the form, it does not seem to register and the tag is not picked up by the form. Am I missing something in my code, is this impossible or, if impossible there a better way to do this? Thanks for any suggestions.

Here is code:

html

<form method="post" action="enterdata.php">
<input type="text" name="text">Enter text here.
<div id="inserttags"></div><a href="javascript:void(0);" onclick="getTags()";>Get tags</a>
<form type="button" name="submit" value="Enter Text and Tag">
</form>

javascript

getTags() { 
various Ajax goes here, then
//following line inserts value into div of html
    document.getElementById("inserttags").innerHTML=xmlhttp.responseText;
// a bit more ajax, then following pulls tag from db 
xmlhttp.open("GET","findtags.php",true);
xmlhttp.send();
} //end function

php

//gettags.php
//first pull tag from db.  Then:
echo 'input type="text" name="tag" value= "html">Enter tag';
//above output gets inserted in div and is visible on page.

Though the above output is visible on page, the form does not seem to pick it up when you click "Enter Text and Tag" to submit the form.

© Stack Overflow or respective owner

Related posts about AJAX

Related posts about forms