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
https://forums.phpfreaks.com/topic/198662-post-without-page-refresh/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.