Jump to content

POST without page refresh


squiblo

Recommended Posts

I am trying to post data to another page (i am trying to make my own shoutbox/chat sort of thing) without refreshing the page, I have done some research and found a few scripts and tried to mix them together and I have come up with the following shown below, my problem is that the data does not even send.

 

ajax.js

var xmlhttp;

function showUser()

{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
	var content = "bghkefgrgy";
	xmlhttp.open("POST","http://localhost/test/response.php", true);
	xmlhttp.setRequestHeader("content-type","application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("content-length", content.length);
	xmlhttp.setRequestHeader("connection","close");
	xmlhttp.send(content);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("response").innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

 

index.php

<html>
<head>
<script type="text/javascript" src="http://localhost/test/ajax.js"></script>
</head>

<body>
<form>
	<input type='button' value='send' onclick="showUser();">
</form>

<div id='response'></div>
</body>

</html>

 

response.php

<?php
print_r($_SERVER);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.