I am stringifieng an object in javascript, and sending it via Jquery post method:
reservation={};
count=0;
$(".reservation").each(function() {
reservation[this.id]=jQuery(this).val();
count++;
if (count==jQuery(".reservation").length)
{
reservationString=JSON.stringify(reservation);
$.post("helper.php", {reservation: reservationString}) .done(function(data) {
The "helper.php" file gets the data, but the quotatian marks are all escaped, so when I try to decode it, I get a json error, with error code 4.
Here is the helper.php file:
$reservation=$_REQUEST['reservation'];
echo $reservation . "\n";
$reservation=json_decode(html_entity_decode($reservation));
I tried alerting out the reservationString in javascript, the string is not escaped there, so most likely the jQuery post method escapes the string for me. What could be the problem, can you guys suggest a solution?
Additional Infos:
This is what I get after alerting out the stringified object in javascript:
{"test":"bubub","Domain":"online","Type":"banner","s2id_CompanyId":"","CompanyId":"-","ContactPersonId":"0"}
This is what I get after echoing out the data in php:
{\"test\":\"bubub\",\"Domain\":\"online\",\"Type\":\"banner\",\"s2id_CompanyId\":\"\",\"CompanyId\":\"-\",\"ContactPersonId\":\"0\"}
This should work:
$reservation=$_REQUEST['reservation'];
echo $reservation . "\n";
$reservation=stripslashes(json_decode(html_entity_decode($reservation));
Related
I'm building a WordPress plugin. The PHP script sends the JSON, javascript receives it. Time to time, the js receives an empty JSON, even when I'm sure it should receive data. If I put an empty echo (echo ' ';), it fixes my problem and the data received correctly.
The receiver:
jQuery.ajax({
url: url + "get_bookings.php",
method: "POST",
data: {
week: t,
year: e
}
}).done(function(w) {
w = JSON.parse(w);
fetchTableData(w)
})
The sender:
echo ' ';
.
. // Code here.
echo json_encode( $query );
I couldn't find why this happens. Sometimes it works fine without the empty echo.
Edit: thanks for the answers, the exit did not help.
I checked the network panel, and it was requesting get_bookings.php twice. I put a slash at the end of the URL, and works fine.
url: url + "get_bookings.php/",
First, remove this line:
echo ' ';
Id suggest debugging the PHP script first by testing the resulting value for $query:
print_r($query);
If the value intermittently results as an empty string, then make sure to test for this and return an empty JSON object instead when necessary.
if (empty($query)) {
$query = (Object)[];
}
echo json_encode($query);
Please remove the second parameter true in json_encode and also issue is header already sent error.
You don't need header for json encode in ajax return. just add json_encode and that's it.
I am making an AJAX GET request using jQuery to a PHP file. I want the PHP script to return a JSON object, however, currently it is returning a JSON string. I realise I can use JSON.parse in the jQuery code, however, any experience I have in making an AJAX call to an API a JSON object is returned. I am trying to do the same with the php script however, it is returning a string as opposed to an object.
Does anyone know what the best practice is here, and if the best practise is to return a JSON object how I would do this using PHP?
Please see the code below:
js
$.get('test.php', function(data){
console.log((data));
});
php
<?php
$jsonAnswer = array('test' => 'true');
echo json_encode($jsonAnswer);
In your PHP file, change the content type to application/json.
JS
$.get('/process.php', function(data) {
console.log(data);
} );
PHP
<?php
header( "Content-type: application/json" );
$jsonAnswer = array('test' => 'true');
echo json_encode($jsonAnswer);
Then your console should read Object {test: "true"} rather than just the JSON string.
Add json to the end of your get function to return json
$.get('test.php', function(data){
console.log((data));
},'json');//here
and/or add this header in php
header('Content-Type: application/json');
more info here
Without modifying PHP script you can do:
$.get( "test.php", function( data ) {
var arr = $.parseJSON(data);
console.log(arr);
alert(arr.test);
});
I have a very short piece of PHP that I use to make HTTP requests from JavaScript.
<?php echo file_get_contents($_GET['url']); ?>
I have used it successfully in a few projects, but am running into a problem with making requests in my current project. Based on my searching, I believe it may be caused by the underscore in the request, though through my searching and not knowing PHP, I have not been able to confirm that.
Below is an example of what I am doing from JavaScript:
$.get("grabber.php?url=" + "http://tidesandcurrents.noaa.gov/api/datagetter?station=8573364&begin_date=20160202&end_date=20160203&product=predictions&units=english&time_zone=gmt&format=json&application=poseidonweathercom+&datum=MLLW", function(forecast) {
console.log(forecast);
});
If I copy the url and put in it in a browser, I get back the JSON that I requested. When I use the code above, I end up getting an error message from NOAA:
Wrong Product : Product cannot be null or empty Wrong Time zone: Time zone cannot be null or empty Wrong Unit:Unit cannot be null or empty Wrong Format: Format cannot be null or empty Wrong Date: The beginDate cannot be null or empty
Do I need to use a regex for the underscore in PHP? Is there some other issue that I do not understand?
Thanks.
You need to send it encoded, which will convert all the underscores/spaces/ampersands etc. with their encoded equivalents:
var url = "http://tidesandcurrents.noaa.gov/api/datagetter?station=8573364&begin_date=20160202&end_date=20160203&product=predictions&units=english&time_zone=gmt&format=json&application=poseidonweathercom+&datum=MLLW";
$.get("grabber.php?url=" + encodeURIComponent(url), function(forecast){
console.log(forecast);
}
Using encodeURIComponent() on that URL shows:
http%3A%2F%2Ftidesandcurrents.noaa.gov%2Fapi%2Fdatagetter%3Fstation%3D8573364%26begin_date%3D20160202%26end_date%3D20160203%26product%3Dpredictions%26units%3Denglish%26time_zone%3Dgmt%26format%3Djson%26application%3Dposeidonweathercom%2B%26datum%3DMLLW
Alternatively, if you just want to access the JSON data and handle it within the JavaScript function, you can retrieve the data via the URL directly, without having to encode the URL:
$.get("http://tidesandcurrents.noaa.gov/api/datagetter?station=8573364&begin_date=20160202&end_date=20160203&product=predictions&units=english&time_zone=gmt&format=json&application=poseidonweathercom+&datum=MLLW", function(forecast) {
console.log(forecast);
});
Um why do you even need your php code ... the code below will work just fine and eliminate your server overhead.
$.get("http://tidesandcurrents.noaa.gov/api/datagetter?station=8573364&begin_date=20160202&end_date=20160203&product=predictions&units=english&time_zone=gmt&format=json&application=poseidonweathercom+&datum=MLLW", function(forecast) {
console.log(forecast);
});
For some reason, when I try to send JSON data to a PHP page (where it gets downloaded as a spreadsheet), it runs without error, but doesn't bring up the prompt to download the spreadsheet. The JSON has generated without problem (I have made the PHP page create the file on the server, before trying to make it download without creating it).
Here is the JavaScript code that sends the JSON data to the server:
function writeToSpreadsheet()
{
// get the json for #theTable
var tableJSON = tableToJSON("tr:not(#titleRow)");
//alert(tableJSON);
alert("Sending table data to be written to the spreadsheet...");
$.post('/ResearchProject/tableContent/exportTable.php', {'table': tableJSON}).done(
function(response) { alert(((response == '') ? response : (tableJSON.title + ' written to file!')));})
.fail(function (xhr, ajaxOptions, thrownError) { alert("ERROR:" + xhr.responseText+" - "+thrownError); });
}
and here is exportTable.php
<?php
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str); // escaping all of the tabs
$str = preg_replace("/\r?\n/", "\\n", $str); // escaping any and all cases of carriage return
// if there is a single double-quote in the string, we wrap the string in quotes, replace every single double-quote with double double-quotes, and
// end with a double-quote
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
// the data is coming from a JSON object that is being sent here
if (isset($_POST['table']))
{
$tableJSON = $_POST['table']; # somehow, this is already a PHP array (exactly the one we need)!!
// get the name of the table from the $tableJSON
$tableName = $tableJSON['title'];
// get the title row from $tableJSON
$titleRow = $tableJSON['titleRow'];
// fix the titleRow
foreach ($titleRow as $heading)
{
$heading = trim(preg_replace('/\s+/', ' ', $heading));
}
// get the rows from $tableJSON
$rows = $tableJSON['rows'];
// form the filename from the tableName
$fileName = $tableName . '.xls';
// here, we download the file without even creating it
header("Content-Disposition: attachment; filename=\"$fileName\"");
header("Content-Type: application/vnd.ms-excel");
// we echo the titleRow first
array_walk($titleRow, 'cleanData');
echo implode(chr(9), $titleRow) . "\r\n";
?>
<script>console.log('Title row written to file.');</script>
<?php
// now we echo the data
foreach($rows as $row)
{
array_walk($row, 'cleanData');
echo implode(chr(9), $row) . "\r\n";
?>
<script>console.log('Data row written to file.');</script>
<?php
}
}
else
{
echo 'You sent me no data :(\n';
}
?>
OK, MikeWarren, how do I test this??
You can test it by selecting a table from the dropdown menu and clicking the "Export table to spreadsheet" button here: http://dinotator.biokdd.org/ResearchProject/tableViewer.php
I am trying to have it where the table that is on the HTML page gets converted into an JSON object, and then downloaded. Thus, I would need to POST the data to the PHP page, right? (Query strings don't work.)
Query strings won't work because you are using jQuery's $.post call which means that your data is sent in the body of the request, as opposed to a query string which is what a GET uses. For JSON you do indeed want to use a POST.
As for what's going wrong, you need to decode your JSON into a PHP array using json_decode. Unfortunately it can't simply handle JSON how it is.
So most likely you'll want to do:
// now a poorly named variable
$tableJSON = json_decode($_POST['table']);
Also, looking at your Ajax, $.post does accept a .fail() listener, but it doesn't pass any error data as part of the callback. So if you want to be able to handle incoming response errors you'll need to use $.ajax:
$.ajax({
type: "POST",
url: "/your/url.php",
dataType: "json",
error: errorCallback
});
Finally, looking at how your code is structured, if you're actually trying to save to file, you're going to need some more logic. Right now, you're just rendering that table, and then returning it as a response which will show up in your done function. You're going to add some more logic in order to make it actually download. This question entails your exact problem.
Good luck!
I have found so much bad advice on the internet about how to solve this problem. One of the answers here also didn't work. :(
I have decided to get advice from a friend of mine, and me and him have decided on this approach:
Have my exportData.php simply write the data to $_SESSION, echo a JSON-encoded "success", and then exit
On exit, on the client-side of things, if "success" has been received, have the JavaScript open up a new tab to a file that I have created called downloadFile.php which actually does the downloading.
Why didn't sending the data between files work?
Downloading data entails setting the right headers and printing the data. When you send data to the file to do this (via AJAX), the buffer that the data is printed to is the one for response. You can see this by saying something like
success: function(response)
{
alert(response);
} and see the data that you "downloaded" not get downloaded, but get printed on-screen.
However, if you go to the file instead of simply passing data to it, your data will download, provided that it has access to the data that you are trying to download. You can see examples of this here: www.the-art-of-web.com/php/dataexport/ . In those examples, the data was "static" (that is, only existing in the scope of that PHP file, until download happened).
We then see that we should let another file handle the downloading. Here is what its contents should look like:
<?php
if (!isset($_SESSION))
session_start();
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str); // escaping all of the tabs
$str = preg_replace("/\r?\n/", "\\n", $str); // escaping any and all cases of carriage return
// if there is a single double-quote in the string, we wrap the string in quotes, replace every single double-quote with double double-quotes, and
// end with a double-quote
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
// get the data from $_SESSION
if (isset($_SESSION))
{
$fileName = $_SESSION['fileName'];
$titleRow = $_SESSION['titleRow'];
$rows = $_SESSION['rows'];
// set the excel headers
header("Content-Type: application/vnd.ms-excel");
//header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$fileName\"");
header("Pragma: no-cache");
header("Expires: 0");
// attempt download
array_walk($titleRow, 'cleanData');
echo implode(chr(9), $titleRow) . "\r\n";
// now we echo the data
foreach($rows as $row)
{
array_walk($row, 'cleanData');
echo implode(chr(9), $row) . "\r\n";
}
}
else
{
die('Problem with session variable. Data could not be sent for download.');
}
exit;
?>
Of course, before doing this, make sure that you have 'fileName', 'titleRow', and 'rows' already written to $_SESSION.
This should help anyone having problem downloading HTML table to Excel spreadsheet via PHP, and the best part is that you don't have to bloat your server by downloading an entire library, for potentially the functionality of one button!!
I've read a few questions about the subject here on but couldn't find the answer I'm looking for.
I'm doing some $.post with jQuery to a PHP5.6 server.
$.post('/', {a:100, b:'test'}, function(data){
}, 'json');
The encoding from the console is
Content-Type application/x-www-form-urlencoded; charset=UTF-8
If I try to read the POST data with a regular $_POST, PHP5.6 alerts me
PHP Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead
So then I've tried the suggestion, added always_populate_raw_post_data = -1 in php.ini and
json_decode(file_get_contents("php://input"));
PHP5.6 alerts me that it is invalid
PHP Warning: First parameter must either be an object or the name of an existing class
So I've dumped file_get_contents("php://input") and it's a string.
a=100&b="test"
So I've parsed the string and encoded then decoded
parse_str(file_get_contents("php://input"), $data);
$data = json_decode(json_encode($data));
var_dump($data);
And THEN I finally have my $data as an object and not an array, as a true JSON object.
I've resorted to keep on using $_POST for now... But then I'm wondering about upgrading PHP..
The question here is that, is there a straighter forward solution to this or does it mean using file_get_contents("php://input") also means doing the parsing decoding encoding shenanigans?
Edit: so it appears this doesn't work either on multi levels json's.
Consider the following:
{"a":100, "b":{"c":"test"}}
As sent in Ajax/Post
{a:100, b:{c:"test"}}
Doing
parse_str(file_get_contents("php://input"), $post);
var_dump($post);
Will output
array(2) {
["a"]=>string(8) "100"
["b"]=>string(16) "{"c":"test"}"
}
Or doing (as suggested)
parse_str(file_get_contents("php://input"), $post);
$post= (object)$post;
Will output
object(stdClass)#11 (2) {
["a"]=>string(8) "100"
["b"]=>string(16) "{"c":"test"}"
}
How do I transform file_get_contents("php://input") into a true object with the same "architecture" without using a recursive function?
Edit2 : My mistake, the suggested worked, I got side tracked in the comments with JSON.stringify which caused the error.
Bottom line: it works with either json_decode(json_encode($post)) or $post=(object)$post;
To recap, using jQuery $.post :
$.post('/', {a:100, b:{c:'test'}}, function(data){
}, 'json');
parse_str(file_get_contents("php://input"), $data);
$data = json_decode(json_encode($data));
or
parse_str(file_get_contents("php://input"), $data);
$data= (object)$data;
No need to use JSON.stringify
Receiving serialized/urlencoded POST data in the request's POST body as you are, you've correctly transformed it into an array with parse_str() already.
However, the step of encoding then decoding JSON in order to transform that into the object (as opposed to array) you're looking for is unnecessary. Instead, PHP will happily cast an associative array into an object of class stdClass:
parse_str(file_get_contents("php://input"), $data);
// Cast it to an object
$data = (object)$data;
var_dump($data);
A little more information is available in the PHP documentation on type casting.
In order to send raw json data, you have to stop jQuery from url-encoding it:
data = {"a":"test", "b":{"c":123}};
$.ajax({
type: 'POST',
url: '...',
data: JSON.stringify(data), // I encode it myself
processData: false // please, jQuery, don't bother
});
On the php side, just read php://input and json_decode it:
$req = file_get_contents("php://input");
$req = json_decode($req);
My mistake, the suggested worked, I got side tracked in the comments with JSON.stringify which caused the error. Bottom line: it works with either json_decode(json_encode($post)) or $post=(object)$post;
The answer Michael gave is correct but side tracked me and I left the error in my code. JSON.stringify is only useful when posting the data from a form as I replied in the comment.