ajax json response containing html - php

Do I need to json_encode ajax response data before sending?
$html1 = '<span>some html</span>';
$html2 = '<span>some html</span>';
$res = array('html1'=>$html1, 'html2'=>$html2);
echo json_encode($res);
or
echo $res;

The X in AJAX stands for XML
If you return XHTML, you do not need to json_encode it
echo '<div>'.$html1.$html2.'</div>';
You can
dump it onto the page someElement.innerHTML = returnValue; or document.body.insertAdjacentHTML('afterend',returnValue);
Extract from a DOMFragment:
const domFragMent = document.createElement("div")
domFragment.innerHTML = returnValue;
const spans = domFragment.querySelectorAll("span");
use a domParser
Send a json_encoded array of values and parse them on the client into spans

Related

Saving data across multiple lines JSON

Currently I have PHP and JS save my data in a JSON.
Below is the code for it:
PHP:
<?php
$jsonString = file_get_contents('passwords.json');
$data = json_decode($jsonString, true);
// get the q parameter from URL
$q = $_REQUEST["q"];
$item = $_REQUEST["item"];
// or if you want to change all entries with activity_code "1"
foreach ($data["passwords"] as $key => $password) {
echo $data["passwords"][$key]['timesToUse'] - 1;
if ($password['password'] == $q) {
$data["passwords"][$key]['timesToUse'] = $data["passwords"][$key]['timesToUse'] - 1;
$data["passwords"][$key]['winningItem'] = $item;
}
}
$newJsonString = json_encode($data);
file_put_contents('passwords.json', $newJsonString);
?>
JS:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "passwords.php?q="+passwords[i].password+"&item="+winningItem.getFullName(), true);
xmlhttp.send();
The issue that I am having is that initially my JSON file looks like this:
{"password":"IS360H","timesToUse":1,"winningItem":""},
{"password":"AGRD33","timesToUse":1,"winningItem":""},
{"password":"4BX0FV","timesToUse":1,"winningItem":""},
{"password":"99NOTX","timesToUse":1,"winningItem":""},
{"password":"X2Z8BO","timesToUse":1,"winningItem":""},
{"password":"R9G4Q0","timesToUse":1,"winningItem":""},
{"password":"CG9RGT","timesToUse":1,"winningItem":""},
As you can see it is nice and neat and is sitting at 1 password per line. However once the file gets modified, it turns to this:
{"password":"IS360H","timesToUse":0,"winningItem":"counterUAV"},{"password":"AGRD33","timesToUse":0,"winningItem":"sentryGun"},"password":"4BX0FV","timesToUse":0,"winningItem":"chopperGunner"},"password":"99NOTX","timesToUse":1,"winningItem":""},{"password":"X2Z8BO","timesToUse":1,"winningItem":""}, {"password":"R9G4Q0","timesToUse":1,"winningItem":""},"password":"CG9RGT","timesToUse":1,"winningItem":""},
What do I need to do to make my JSON file preserve the line breaks?
Thank you!
There is option JSON_PRETTY_PRINT available in json_encode function, if you are using same for encoding json. Try following:
$json_string = json_encode($data, JSON_PRETTY_PRINT);
Read more about json_encode

how to unserialize jquery serialized data in php?

I have sent the serialized data to PHP on form submission:
$('#new-store-pickup').on("click",function(){
var businessHoursManager = $("#businessHoursContainer3").businessHours();
$('#businesshourvalue').val(JSON.stringify(businessHoursManager.serialize()));
$('#new-product-form').submit();
return false;
});
The data format is below:
[{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":false,"timeFrom":null,"timeTill":null},{"isActive":false,"timeFrom":null,"timeTill":null}]
How do I unserialize the above data in PHP?
Simple use json_decode for decode json data in php
<?php
$json = '[{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":false,"timeFrom":null,"timeTill":null},{"isActive":false,"timeFrom":null,"timeTill":null}]' ;
echo "<pre>";
$converted = json_decode($json);
print_r($converted);
?>

HOW TO PHP JSON DATA convert PHP Treeview

Hi Guys I have a JSON data I need to convert this data Treeview a json data url: http://torrent2dl.ml/json.php
recovered state = http://torrent2dl.ml/json.php?tree
I tried to do http://torrent2dl.ml/hedef.php
how to convert this data a php function or code ?
json_decode($jsonObject, true);
Use json_decode() :
<?php
$url = 'http://torrent2dl.ml/json.php';
$JSON = file_get_contents($url);
// echo the JSON (you can echo this to JavaScript to use it there)
echo $JSON;
// You can decode it to process it in PHP
$data = json_decode($JSON);
var_dump($data);
?>
Source : https://stackoverflow.com/a/8344667/4652564

How to parse JSON in PHP

I'm trying to parse JSON sent by POST to my webservice.
I'm using Advanced Rest Client tool for Google Chrome to test restapi.
How can I read this request and response to it?
I'm sending key called "format" and "json" as value for this key. I'm adding JSON like
"{"id":"235325"}"
Part of my PHP API code:
if( strcasecmp($format,'json') == 0 )
{
//how to read that id = 235325?
}
Try the json_decode() function. It is the standard function to parse json in php.
If want to work with array:
$json = '{"id":"235325"}';
$array = json_decode($json, true);
foreach($array as $element){
if($element == 0){
}
}
With object:
$json = '{"id":"235325"}';
$object = json_decode($json);
if($object->id == 0){
}

remove string in json that contains value via jquery or php

I have json string, which is produced by server.php
[{"attr":{"id":"node_7","rel":"default"},"data":"doc.html","state":""},
{"attr":{"id":"node_8","rel":"folder"},"data":"New node","state":"closed"},
{"attr":{"id":"node_9","rel":"folder"},"data":"New node","state":""}]
How do I remove a full string that contains the value rel=default
This is the code I have for server.php.
require_once("config.php");
$jstree = new json_tree();
echo $jstree->{$_REQUEST["operation"]}($_REQUEST);
die();
Using PHP:
// convert json string to array
$json = json_decode($json_string);
// filter out items
$json = array_filter($json, function($item)
{
return $item->attr->rel != "default";
});
// convert back to string
$json_string = json_encode($json);
You can use data.items.splice(X, Y); to remove an element ;)

Categories