0

I have a page php with this code:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=drawing,geometry"></script>

<script type="text/javascript"> 

console.log(" cap in <?php print $_POST['CAP'] ?>");

var v_lat_to = '<?php print $_POST['LAT_TO'].$_GET['LAT_TO'] ?>';
var v_lng_to = '<?php print $_POST['LNG_TO'].$_GET['LNG_TO'] ?>';
var v_lat_from = '<?php print $_POST['LAT_FROM'].$_GET['LAT_FROM'] ?>';
var v_lng_from = '<?php print $_POST['LNG_FROM'].$_GET['LNG_FROM'] ?>';
var v_cap_from = '<?php print $_POST['CAP'].$_GET['CAP'] ?>';
var v_npax = '<?php print $_POST['N_PAX'].$_GET['N_PAX'] ?>';
var v_ora =  '<?php print $_POST['ORA'].$_GET['ORA'] ?>';
var v_min =  '<?php print $_POST['MIN'].$_GET['MIN'] ?>';

var v_price = 0;
var v_result = "OK";

if(v_lat_to == '' && v_lng_to == ''){
 v_result = "KO - Coordinate di destinazione non valorizzate"; 
 console.log("KO - Coordinate di destinazione non valorizzate");
}
else if(v_cap_from == "" && v_lat_from == "" && v_lng_from == ""){
 v_result = "KO - Coordinate di partenza non valorizzate"; 
 console.log("KO - Coordinate di partenza non valorizzate");
}

/*
* ...
* ...
* others code with logic javascript
*
*/

// return the output with object json
var jsonObj = [];
var item = {};
item["price"]=v_price;
item["result"]=v_result;

jsonObj.push(item);

document.write(JSON.stringify(jsonObj));

</script>

When I call this php page, I dont find in output the object json, but there is the full code javascript.

Is's correct how create the json from javascript?

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
Badda
  • 1
  • 1
  • possible duplicate of [Create JSON object dynamically via JavaScript (Without concate strings)](http://stackoverflow.com/questions/16507222/create-json-object-dynamically-via-javascript-without-concate-strings) – Naruto Mar 23 '15 at 10:10
  • Why are you doing this? Why not output the JSON string using PHP? – Styphon Mar 23 '15 at 10:14
  • I dont use php because in my code js i have to call API JS google.maps.geometry – Badda Mar 23 '15 at 10:58

1 Answers1

0
[{"price":0,"result":"KO - Coordinate di destinazione non valorizzate"}]

Is this what you want? You're seeing the full javascript because you are not calling that php file via a webserver. I think you're trying to access it directly like a local file.

Sasi Varunan
  • 2,806
  • 1
  • 23
  • 34