1

In my HTML I have this line of code:

<td>Username:</td>
<td><input type="text" size="40" id="username" name="username" /></td>
<td><input type="button" value="check" onclick="checkExistenceUsername()" /></td>
<td id="result_of_checking"></td>   

How I tell to ajax that he has to take the content of the tag with id "username"?

This is javascript:

function checkExistenceUsername() 

{ var xmlhttp;

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("result_of_checking").innerHTML = xmlhttp.responseText;
    }
}

xmlhttp.open("GET", "check_existence.php", true);
xmlhttp.send();

}

m59
  • 43,214
  • 14
  • 119
  • 136
newtphp
  • 245
  • 1
  • 4
  • 13
  • http://stackoverflow.com/questions/11563638/javascript-get-input-text-value – koosa Dec 15 '13 at 16:51
  • Thanks I already knows that (DOM access...). I need to know where to place the code "document.getElementById("username").value" in the javascript code for ajax. (i have not been clear in the title, now i fixed it ) – newtphp Dec 15 '13 at 16:54

1 Answers1

1

I think you would need something like this answer

But instead of hard coded parameters you would need to add the input variable:

var params = "myvar="+encodeURIComponent(document.getElementById('username').value);
Community
  • 1
  • 1
koosa
  • 2,966
  • 3
  • 31
  • 46
  • I have to put the text in "xmlhttp.send(VARIABLE_WITH_TEXT_HERE);" ?? – newtphp Dec 15 '13 at 17:01
  • THANKS FIXED!! The key is this line of code and i decided to sent data with POST not anymore with GET. request.setRequestHeader("Content-type", "application/x-www-form-urlencoded") http://coursesweb.net/ajax/ajax-post-php – newtphp Dec 15 '13 at 17:57