xmlhttprequest and php - php

Here's a stupid question, don't laugh, I've been trying to figure this out but no luck, I guess I'm too tired. So there's an application sending data (image file as string) to a php script, sending it with XMLHttpRequest.send(data). My question is how do I access this string in PHP to save it to a file?
EDIT $_POST, $_GET and $_FILES are all empty

First: Make sure you are sending a POST request (when you call the open method)
Second: Since it looks like you are sending raw data, set an appropriate content type (with setRequestHeader)
Third: You should be able to retrieve the data with $HTTP_RAW_POST_DATA or file_get_contents("php://input");.

Related

server not parsing php file called from jquery ajax

Im using a basic jquery ajax call.
I make a call to a php file without input parameters with option datatype set to json.
I want the server to parse the php which queries a table in a mysql db, convert it to array and finally encode it to json and return.
I tried a test call from the browser by copying the php file url in the address field, and it shows that it works, since I can see a blank page with all the rows of the table in json formatting.
Instead, when calling from my javascript code the $.ajax call fails with error
Requested JSON parse failed
which means ajax call was expecting json (since I set option datatype to that) but received another format.
So I tried removing the datatype option from the call, and lo and behold I got a response success, but what did I received from my php file?
Well, it was the whole code in the file, like the server doesn't parse it cause it thinks it's plain text.
Is there a way out of this problem? Thanks.
Send also content header with json data
<?php
header('Content-Type: application/json');
echo json_encode($data);
The ajax function is expecting a JSON encoded document so you have to send a header with the response saying that the response contains JSON. Something like this:
<?php
header('Content-Type: application/json');
// All your code here
echo json_encode($someArray);
?>
I'm an idiot, I may answer my own question, I was debugging jquery from visual studio, which automatically instatiates an iis web server, and it explains why it was treating php files like text files. Obviously under apache everything works fine.
Sorry for taking your time....

passing variable value from javascript to php

I am working on a project where I used ajax for asynchronous DB access.
I store the value in JavaScript variable as follows..
var content=xmlhttp.responseText;
now what I wanted is to pass this value to the php module on same page..
Please suggest me..its urgent
You'll have to make a separate AJAX request to another script to achieve this. PHP is server-sided and therefore cannot directly interact with the client.
You should handle the data (which you are assigning to content) in PHP because, as the other answers here tell you, PHP is server-side and JavaScript is on the client. If you are getting this data from a page you control, instead of var content = xhr.responseText; just modify the data BEFORE you send it. For example, if you are making an AJAX call to a process.php file on your server to get the data you are otherwise assigning to content in JavaScript, be sure to handle the data in process.php PRIOR to echo()'ing the data (which you are then storing inside content on the client):
In process.php:
// below is the normal server script which you are storing in content on the client
// echo $result;
// instead, we are going to operate on the data first:
return doSomething($result);
Then on the client:
var newContent = xhr.responseText;
And the newContent variable will contain the data you previously wished to modify with PHP. If you DO NOT have control of the server script which you are calling with AJAX, then as mentioned here already, you will need to send a SECOND AJAX call to the server with your PHP, and use $_GET or $_POST to retrieve that content data and then play with it there.
Am unclear about your need to pass value from javascript to php.
But I can give you,
A non-recommended but working approach towards your problem:
You said, you are making an Ajax call at first. While processing the corresponding server side php function, Store the response value (value of xmlhttp.responseText) into a $_SESSION variable. Finally Reload the page (using location.reload()) inside the ajax response handler function.
And a recommended approach towards your problem:
You might have added some if-else control-flow structures in the php code and expecting to execute them after getting the ajax response value (sadly you cannot do that). So if you do have some logic like that, then convert those if-else conditions to corresponding Javascript code. May be a javascript function and call that function by passing the ajax response value to it. This new function will use your ajax response value and make changes in some parts of your webpage by applying necessary logic.

how to send json to php file to save on server

Im developing a javascript/php/ajax application and have stumbled across some problems.
Im confident at javascript and ajax but my php is a little rusty.
My aim is to get a json file from the server, modify it using javascript, and then save this modified json back to the server.
After some research it was obvious that the modified json file must be sent to a php file, this php file will then do the saving.
HOW do i send a json string from javascript to php? OR how do I get the php file to pick up the json variable from the javascript?
im assuming the php will be something like:
<?php
$json = $_POST["something"];
?>
but how do i send or "post" the javascript variable to the php file?
I was thinking about creating a hidden html form, setting a hidden textbox to the json string, then posting the html form
But surely there must be a way without including the html middle man?
Im looking for answer WITHOUT jQuery. Seeing as this is an application i would like a relieve the dependancy of jQuery.
The only, as far as I know, proper way to do this is sending data to a file through Ajax, then attaching some POST or GET data.
Example:
You could send the data in the url as GET:
http://example.com/foo.php?myvalue=cake
And then in the php you'd say:
<?php
$yourvalue = $_GET['myvalue'];
// Some code to save it to the database/server
?>
You would need to create a XMLHttp-Request like this:
xmlhttp.open( "POST", url, false );
xmlhttp.setRequestHeader(
'Content-Type',
'application/x-www-form-urlencoded; charset=UTF-8'
);
xmlhttp.send("data=yaystuff!")
So you can send it. In PHP just get the $_POST['data'] variable and do some json_decode() on it, if you send JSON :)

Location of information sent by POST to a php file via HTTPRequestObject.send function

Where does the HTTPRequestObject place strings I have sent via "POST" to a php file? I have tried looking in the $_POST and $_REQUEST arrays.
I am using send like follows:
request.send("name="+name+"&comment="+comment);
from javascript to send data to a php file, where I loose it. The request goes though (I check with onreadystatechange), and I'm using the POST method in my open call.
Try this:
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
before request.send()
and you should replace:
request.send("name="+name+"&comment="+comment);
with:
request.send("name="+encodeURIComponent(name)+"&comment="+encodeURIComponent(comment));
"form-urlencoded" does not refer to an associative array. I think "request.send("name="+name+"&comment="+comment);" is correct.

Can user send HTTP_RAW_POST_DATA to my site?

A stupid question but I am a little confused.
I use pubsubhubbub and check for a new information with if(isset($_HTTP_RAW_POST_DATA)).
I check if user is logged with:
if(isset($_SESSION['user'])) {
//logged
}
Can a user send a HTTP_RAW_POST_DATA?
So, basically anyone who sends an HTTP POST request to your callback will actually send a $HTTP_RAW_POST_DATA. Many languages and framework have libraries to parse this into HTTP POST params.
In the context of PubSubHubbub, the body is NOT made of params, so you have to use the lower level $HTTP_RAW_POST_DATA, as parsing the XML as params would not make any sense.
If you're trying to secure your callback URL, there are multiples ways to do it:
Make your callback URLs unique and un-guessable : for example, use a unique internal identifier in the URLs for each feed to which you subscribed.
Subscribe using http*s*, and by providing a hub.secret. This secret will then be used by the hub to compute a unique signature for each notification. You have to make sure this signature matches the content that you get. Read more about this here.
The raw post data is the data that is used to extract the POST parameters that can be accessed by $_POST. An user can also post un-parametarized data with post, yes.
A user will always send raw POST data to your scripts. PHP will then parse it and populate $_POST. When POSTing from a form, $_POST is equivalent to:
parse_str($HTTP_RAW_POST_DATA, $data);
var_dump($_POST);
var_dump($data); // yields the same as $_POST
However, if you really want to fetch the raw POST data, the preferred way is:
$rawPost = file_get_contents('php://input');
... because $HTTP_RAW_POST_DATA relies on the always_populate_raw_post_data INI setting, and also because it won't work with multipart/form-data content type.

Categories