Zalif
Zalif

Reputation: 125

Javascript in PHP file which outputs JSON

Is there a way to use Javascript code inside a PHP file that uses header('Content-Type: application/json'); to output in JSON format?

EDIT: I'm trying to change the color of a css class when $est = 'Crest'but I get the javascript code printed along. Javascript part is inside comment /*HERE*/

<?php
header('Content-Type: application/json');

$vs=array();
$vs1=array();

include("json/connectorcl.php");

if ((isset ($_GET['ty'])) and (isset ($_GET['est']))){
    $nprocesso = $_GET['ty'];
    $est =  $_GET['est'];


if ($est == 'Crest') {
$query2 = "SELECT * FROM PATERN WHERE CREST='1'";
$result2 = oci_parse($connect, $query2);
oci_execute($result2);

/*HERE*/
echo "<script type='text/javascript'>
$('.time-title').css({'color':'blue'});</script>";
/*HERE*/
}
ELSE  {
$query2 = "SELECT * FROM PATERN";
$result2 = oci_parse($connect, $query2);
oci_execute($result2);
}

while($res2 = oci_fetch_array($result2) AND $res5 = oci_fetch_array($result5))  {
 $a++;   
 $vs['id']= $a;
 $vs['title']='VS - '.$res2['CATEGORIA_DESC'];
 $vs['startdate']=$res2['DATAMSG'];
 $vs['enddate']=$res2['DATAMSG'];
 $vs['description']= '1ºH - '.$res2['VALOR'].'| MAX - '.$res5['MAXVAL'].'| MIN - '.$res5['MINVAL'].'| AVG - '.$res5['AVGVAL'];
 $vs['date_display']='ho';
 $vs['icon']='plus_blue.png';
 $vs['importance']='30';

 $b=$a;
 array_push($vs1,$vs);
}

echo str_replace(array('[', ']'), '', htmlspecialchars(json_encode($vs1), ENT_NOQUOTES));}}

Upvotes: 0

Views: 41

Answers (2)

Happy Coding
Happy Coding

Reputation: 2525

header('Content-Type: application/json');

echo json_encode(array('text' => CONTENT_TO_BE_FETCHED));

But if you want to change the color of the css, try something like this :

$vs['css']= 'blue';

Upvotes: 0

user2946316
user2946316

Reputation:

If i understand you question right, yes you can.

Example, json.php

header('Content-Type: application/json');
echo json_encode(array('text' => 'im json baby'));

Upvotes: 1

Related Questions