views:

269

answers:

4

I would like to be able to read XMLHttpRequest that is sent to a PHP page. I am using prototype's Ajax.Request function, and I am sending a simple XML structure.

When trying to print the POST array on the PHP page, I don't get any output.

Any help appreciated.

EDIT: Below is my code

<html>
<head>

<SCRIPT type="text/javascript" src="prototype.js"></script>

</head>
<body>

<script type="text/javascript">

var xml='<?xml version=1.0 encoding=UTF-8?>';
xml=xml+'<notification>';
xml=xml+'heya there';
xml=xml+'</notification>';
xml=xml+'</xml>';


var myAjax = new Ajax.Request('http://localhost:8080/post2.php',{
    contentType: 'text/xml',
    parameters: xml,
    method: 'post',
    onSuccess: function(transport){ alert(transport.status); alert(transport.responseText); }
});

</script>

</body>
</html>

post2.php

Welcome <?php print_r($_POST); ?>!<br />
+1  A: 

fopen with input:// wrapper or $HTTP_RAW_POST_DATA

Col. Shrapnel
What the Col is referring to is here: http://us2.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data
Joseph
+3  A: 

You will read it exactly the sme way you read normal request vars.

$_GET['varname'] and $_POST['varname']

chris12892
Unless I'm doing something wrong, I get nothing but an empty array when I print_r $_POST. In my ajax call, my parameter should only be the xml string in the format '<xml>...</xml>, right? Thank you for your input.
Adrian
You might try to `print_r($_GET)`, just to see if somehow it's getting send via GET.
chris12892
A: 

When you use "method: post" and you want to send a post body, you need the parameter postBody. So something like this might work:

var myAjax = new Ajax.Request('http://localhost:8080/post2.php',{
    contentType: 'text/xml',
    postBody: xml,
    method: 'post',
    onSuccess: function(transport){
        alert(transport.status); alert(transport.responseText);
    }
});

But why did you build a XML doc around your content? You can simply sent the message "heya there" with the postBody without the XML arround.

Edit: Here you'll find all the Ajax.Request options: http://www.prototypejs.org/api/ajax/options

Kau-Boy
A: 

php://input allows you to read raw POST data like

Welcome <?php print(file_get_contents('php://input')); ?>!<br />

Note: php://input is not available with enctype="multipart/form-data".

Ramesh Tabarna