0

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));}}
5
  • Please share with us the code that you're using. Commented Jun 29, 2015 at 15:56
  • Could you show us the code you want to use? Commented Jun 29, 2015 at 15:57
  • Well repair you error within javascript. Commented Jun 29, 2015 at 15:58
  • Just updated question. Hope makes it more understandable. Commented Jun 29, 2015 at 16:19
  • If you try to echo out javascript, then you will not have valid JSON. Also, you shouldn't be stripped out array indicators in teh JSON, you are going to make invalid JSON by doing so. Finally, you should not be HTML-encoding the JSON after it is formed, again you are going to make invalid JSON. You can HTML-encode your data on the client side. Your JSON feed should give only JSON data, not try to format for display as well. You are having a fundamental problem in trying to mix your data feed with display concerns and are going to end up with a mess. Commented Jun 29, 2015 at 17:30

2 Answers 2

1

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'));
Sign up to request clarification or add additional context in comments.

2 Comments

That is not what I'm trying to do. I want to use a javascript script inside a PHP file that outputs in json format.
javascript is run on the client while the php file is run on the server, which returns html if you choose to, but first after its executed. I'm afraid you have to narrow down your problem and be more specific about what you want to achieve.
0

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';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.