0
function state()
{
var x=document.getElementById("count").value;
document.write(x);
 if(Window.XMLHttpRequest)
{
   xmlhttp=new XMLHttpRequest();

}
else
{
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
  if(xmlhttp.readystate==4 && xmlhttp.status==200)
  {
   document.getElementById("response").innerHTML=xmlhttp.responseText;
  }
  xmlhttp.open("GET","getdata.php?val=".+x,"true");
  xmlhttp.send();
}
}

This is myfirst time using ajax....i wanted to send the value in 'x' to getdata.php...........function state() is triggered when i choose a value from dropdown and click a button.....document.write() is working if xmlHttpRequest object is not created...and how do i know if the value in string x is being passed to getdata.php...please answer...

2
  • 1
    If you're calling your function after the page has been parsed, document.write() will wipe out all the content of the page and create a new blank document before writing. Commented Mar 29, 2014 at 13:50
  • "how do i know if the value in string x is being passed ", log the $_GET array in your php script to see if it is being passed, or look in your network tab of the dev tools of your particular browser. If you need to log information on the browser side use console.log function, it will print messages to the console of the dev tools. Commented Mar 29, 2014 at 13:51

1 Answer 1

2

You should not do document.write(). You can rather assign the value to some other html tags like span or something.

<span id="countSpan"></span>

Then you can assign the value to that span,

var x=$("#count").val();
$("#countSpan").html(x);

Edit You can use jquery ajax too. The success event will get fired when you get a successful response from the server.

function state() {
    var x = $("#count").val();
    $("#countSpan").html(x);

    $.ajax({
        type: "GET",
        url: "getdata.php?val=" + x,
        success: function (data) {
            $("#response").html(data)
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

'code'function state(){ var x=document.getElementById("count").value; document.write(x);}'code' returns the value of x to the browser..but when i create XMLHttpRequest object as shown above i cannot return the value of x..im doing it for testing purpose as i told im new to this concept..Could you tell me why the value of x is not being returned when XMLHttpRequest object is created?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.