在函数中发送一个php变量来解决javascript问题

时间:2012-02-10 08:26:49

标签: php javascript function

我有函数get_path_dis(),我想将结果发送到javascript和警报结果。打开页面时,警报不会显示。谁能告诉我哪里弄错了

function get_path_dis($array){

//print_r($array);

$total = count($array);
$current = $array[0]['distance'];
$loop=0;

//get shortest distance
for($loop=1;$loop<$total;$loop++){

 $next = $array[($loop)]['distance'];

if ($next<$current){

    $current = $next;
    //print_r('<pre>');

    }
}

//shortest path array;
for($i=0;$i<$total;$i++){

    if ($current==$array[$i]['distance'])

    {
        $xmlConv = $array[$i];
    }
}

$x = json_encode($xmlConv);


?>


 function get_path_dis($array){

  //print_r($array);

$total = count($array);
$current = $array[0]['distance'];
$loop=0;

 //get shortest distance
for($loop=1;$loop<$total;$loop++){

 $next = $array[($loop)]['distance'];

if ($next<$current){

    $current = $next;
    //print_r('<pre>');

    }
}

//shortest path array;
for($i=0;$i<$total;$i++){

    if ($current==$array[$i]['distance'])

    {
        $xmlConv = $array[$i];
    }
}

$x = json_encode($xmlConv);


?> 



 <script type="Javascript">

 <!--
var 
$json_val= "<?php echo($x);?>";
alert($json_val);

//-->

 </script>

1 个答案:

答案 0 :(得分:0)

需要封装在php开始和结束标记中。

alert('<?php echo json_val; ?>');