Jump to content

ajax post method


Petran76

Recommended Posts

Hello everyone and good u.s. east cost morning.

 

I am starting with ajax and I wrote the following javascript code. It seems to work but in my php script the $_POST array it is empty.Any idea.

 

 

Appreciate.

 

 

function ajaxRegister() {

  var http = false;

 

if(navigator.appName == "Microsoft Internet Explorer") {

  http = new ActiveXObject("Microsoft.XMLHTTP");

} else {

  http = new XMLHttpRequest();

}

 

  http.open("POST", "ajax.php", true);

  http.onreadystatechange=function() {

  if(http.readyState == 4) {

  document.getElementById("email").value = http.responseText;

  }

  }

  var data ="petros=1";

  http.send(data);

}

 

 

<?php

 

foreach ($_POST as $key=>$value)

{echo $key ." ".value;}

echo $_POST['petros'];

 

?>

Link to comment
Share on other sites

You need to create post variables, like so (they look the same as get variables):

 

va = 'value1=cat&value2=dog&value3=bird';

 

 

The whole thing:

 

function updateTeams(id, i){
var va = 'value1=cat&value2=dog&value3=bird';
var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
var ajaxRequest;
try{
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			alert("Your Browser Doesn't support AJAX.");
			return false;
		}
	}
}
ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		document.getElementById('email').innerHTML = ajaxRequest.responseText;
	}
}
ajaxRequest.open("POST", 'process/event/addTeam', true);
ajaxRequest.setRequestHeader("Content-Type", contentType);
ajaxRequest.send(va);
}

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.