SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast user9's Avatar
    Join Date
    Jan 2005
    Location
    localhost
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xmlhttp client server usage with POST method

    I'm having a slight problem with receiving data from server by xml-http request using the POST method in conjunction with php.

    I've been using the GET method as described at http://jibbering.com/2002/4/httprequest.html

    I also looked at dhtmlgod's post at the following location http://www.sitepoint.com/forums/show...threadid=49339

    but I'm having trouble modifying my code to fit the POST process.

    This is what I have so far

    js script in use: http://jibbering.com/2002/4/xmlhttp.js

    client:
    Code:
    time=timestamp(); //call to timestamp function
    
    xmlhttp.open("POST", "newsserver.php", false)
    xmlhttp.SetRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.Send("newsTxt="+news+"&time="+time); //news is a string 
    	
    	
    	xmlhttp.onreadystatechange = function() 
    	{
    		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
    		{
    				alert(xmlhttp.responseText);
    		}
    	}
    	xmlhttp.send(null);

    server
    Code:
    <?php
    	// Get message
    	$recNews = $_POST['newsTxt'];  
    	
    	$time=$_POST['time']; //does GET need to be changed to POST?
    	
    	echo $recNews;
    ?>
    Does anyone know what I'm doing incorrectly?

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What errors do you get in the Javascript Console?

  3. #3
    SitePoint Enthusiast user9's Avatar
    Join Date
    Jan 2005
    Location
    localhost
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried to simplify the problem a little.

    Original code from http://www.webpronews.com/webdevelop...POnTheFly.html

    script.js is still http://jibbering.com/2002/4/xmlhttp.js

    client
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
    <html>
    <head>
    <title>Example 2</title>
    <script src="script.js" type="text/javascript"></script>
    <script type="text/javascript">
         function calc() {
           num1 = document.getElementById('num1').value;
           num2 = document.getElementById('num2').value;
    
           var element = document.getElementById('answer');
           xmlhttp.open("POST", "server.php", true);
    	   
           xmlhttp.SetRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
           xmlhttp.Send("num1="+num1+"&num2="+num2); 
    	   
           xmlhttp.onreadystatechange = function() {
               if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
               	element.value = xmlhttp.responseText;
               }
           }
         //xmlhttp.send(null); 
         }
    </script>
    </head>
    <body>
     Enter integers
    <br />
    <form> 
    	<input type="text" id="num1" size="3" /> 
    	+
    	<input type="text" id="num2" size="3" /> 
    	=
    	<input type="text" id="answer" size="5" /> 
    	<input type="button" value="Calculate!" OnClick="calc();" /> 
    </form> 
    </body>
    </html>

    server.php

    Code:
    <?php
    
    // Get numbers
    $num1 = intval($_POST['num1']);
    $num2 = intval($_POST['num2']);
    
    // Return answer
    echo ($num1 + $num2);
    
    ?>
    The above works in IE but not in Firefox. It seems to fail after the call SetRequestHeader. Does anyone know how to fix it?

  4. #4
    SitePoint Enthusiast user9's Avatar
    Join Date
    Jan 2005
    Location
    localhost
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria
    What errors do you get in the Javascript Console?
    Thanks for the reply. I get SetRequestHeader is not a function in firefox.

  5. #5
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mozilla is case-sensitive, it's setRequestHeader and send.

  6. #6
    SitePoint Enthusiast user9's Avatar
    Join Date
    Jan 2005
    Location
    localhost
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Saturn
    Mozilla is case-sensitive, it's setRequestHeader and send.
    Perfect, thank you so much, it works now. I was reading a site geared towards ASP and xml-http, that's how I got the uppercase letter

    Thanks again everyone.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •