Why won'y my AJAX post method work?

Discussion in 'Programming' started by Imozeb, Feb 28, 2010.

  1. #1
    Why won't my AJAX post method work? It gives me an javascript error "not implemented"

    This is what I coded...

    Javascript Code:

    //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");
    }
    
    //printtest
    function printtest()
    {
    	if (xmlhttp.readyState == 4)
    	{
    		document.getElementById('text').innerHTML = xmlhttp.responseText;
    	}
    }
    
    //submit
    function submit()
    {
    	var inputtext = 'inputtext=' + document.getElementById('inputbox').value;
    	var params = inputtext;
    	
    	xmlhttp.open('POST','testfunction.php',true);
    	
    	xmlhttp.onreadystatechange = printtest();
    
    
    	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	xmlhttp.setRequestHeader("Content-length", params.length);
    	xmlhttp.setRequestHeader("Connection", "close");
    	
    	xmlhttp.send(params);
    }
    Code (markup):
    //PHP code:

    <?PHP
    	$var = $_POST['inputtext'];
    	echo $var;
    ?>
    Code (markup):
    Thanks for any help.

    ~imozeb :)
     
    Imozeb, Feb 28, 2010 IP
  2. gustavorg

    gustavorg Active Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #2
    Not sure. Seems ok. Use this code, test it and check it works. Then make any adjustment you need for your script.
    
    var http = getXmlHttpObject(); 
    function handleHttpResponse(){ 
        if (http.readyState == 4){ 
            results = http.responseText; 
            document.getElementById('text').innerHTML = results;
        }
    } 
    
    //submit
    function submit()
    {  
    	
        host = "www.somehost.com";
    	url = "http://" + host + "/script.php";
    	http.open("GET", url, true); 
    	http.onreadystatechange = handleHttpResponse; 
    	http.send(null);
    }
    
    function getXmlHttpObject(){ 
        var xmlhttp; 
    
        /*@cc_on 
        @if (@_jscript_version >= 5) 
        try{ 
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
        } 
        catch (e){ 
        try{ 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        catch (e){ 
        xmlhttp = false; 
        } 
        } 
        @else 
        xmlhttp = false; 
        @end @*/ 
    
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){ 
            try{ 
                xmlhttp = new XMLHttpRequest(); 
            } 
            catch (e){ 
                xmlhttp = false; 
            } 
        } 
        return xmlhttp; 
    } 
    
    Code (markup):
     
    gustavorg, Feb 28, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Your code is using the 'get' method. I can use the 'get' method just fine, but when I try to use the post method everything goes wrong. Can someone help me with the 'post' method?
     
    Imozeb, Feb 28, 2010 IP