0

My code looks like this:

<html>
<body>

<script language="javascript" type="text/javascript">
function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!
        ajaxRequest = new XMLHttpRequest();
    var age = document.getElementById('age').value;
    var queryString = "?age=" + age;
    ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
    ajaxRequest.send(null); 
}
</script>

<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>
</body>
</html>

I used Ajax to stop redirection but it is still refreshing, how can I stop it?

orlp
  • 112,504
  • 36
  • 218
  • 315
HRMNS
  • 60
  • 9
  • 1
    That code should not cause a page refresh. – Quentin Apr 30 '12 at 12:40
  • Can you explain in a bit more detail - what is happening ?? and what you expect to happen ?? – Manse Apr 30 '12 at 12:40
  • i am having a button after scroling , when i click it it goes to top like refreshing , help !! , i want to change simple text of button and submit like above – HRMNS Apr 30 '12 at 12:56
  • hey i got it , it's navigating to top , not refreshing .. hElp how to sttop it at same place .. plzz – HRMNS Apr 30 '12 at 13:06

2 Answers2

4

You could try adding return false;

<input type='button' onclick='ajaxFunction();return false' value='Query MySQL' />
  • I have been trying many things as suggested in many other posts but yours is the only one working, thank you. I can't believe people didn't give you upvotes! – Engin Yapici Aug 25 '12 at 22:21
1

Well that codes doesn't refreshing for me but still i'm not good with ajax did you checked error console or try jQuery you can do it easily with it

<script language="javascript" type="text/javascript">

function ajaxFunction(){
    var age = $("#age").val();
    $.get("ajax-example.php",{"age":age},function(r){ /*Do something with response*/ });
}
</script>
Max Age: <input type='text' id='age' /> <br />
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</body>
</html>
Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52
  • i am having a button after scroling , when i click it it goes to top like refreshing , help !! , i want to change simple text of button and submit like above – HRMNS Apr 30 '12 at 12:59
  • which browser you're using ??? its not scrolling to top in my browser or you've any other code which is making this problem – Mohit Bumb Apr 30 '12 at 13:06