Why won't my AJAX script work?

Discussion in 'JavaScript' started by Imozeb, Feb 27, 2010.

  1. #1
    Why won't this AJAX script work?


    Javascript part:

    //get xmlhttp
    xmlhttp = null;
    if (window.XMLHttpRequest)
    {
    // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
    }
    else
    {
    // code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    //print
    function print()
    {
    	if (xmlhttp.readyState == 4)
    	{
    		document.getElementById('textbox').innerHTML = xmlhttp.responseText;
    	}
    }
    
    //on submit
    function submit()
    {
    	xmlhttp.open('POST','function.php',true);
    	var text = document.getElementById('textbox').value;
    	var param = text;
    	xmlhttp.send(param);
    	xmlhttp.onreadystatechange = print();
    }
    Code (markup):
    PHP code:

    <?PHP
    $var = $_POST['param'];
    echo $var;
    ?>
    Code (markup):

     
    Imozeb, Feb 27, 2010 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    [QUOTE]xmlhttp.onreadystatechange = print();[/QUOTE]
    Code (markup):
    xmlhttp.onreadystatechange = print;
    Code (markup):
    Except don't call it print because that's already a property of window.
     
    Logic Ali, Mar 2, 2010 IP