Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive XMLHttpRequest with PHP?

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 />
like image 704
screenshot345 Avatar asked Nov 16 '25 03:11

screenshot345


2 Answers

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

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

like image 195
chris12892 Avatar answered Nov 17 '25 19:11

chris12892


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".

like image 30
Ramesh Tabarna Avatar answered Nov 17 '25 18:11

Ramesh Tabarna