在JavaScript中引用PHP [英] Referencing php in javascript

查看:81
本文介绍了在JavaScript中引用PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用一个php代码,该代码从mysql表中检索数据并将其填充到数组中,并在javascript中进行访问.但是屏幕上没有显示任何信息,我确定可以肯定的是php不会填充数据,并且在javascript中引用php变量时我已经包含了该文件,并且该文件仅在我的Eclipse(juno)项目中.但是,为什么我会遇到这个问题?

I am trying to call a php code which retrieves data from mysql table and populate it to an array and accessing it in the javascript. But screen shows nothing I know for sure there is no problem with php in populating the data, and referencing the php variable in javascript I have included the file and the file is in my Eclipse (juno) project only. But still why I get this problem?

有什么建议吗?

sample.jsp :

<div id="placeholder" style="width:600px;height:300px;"></div>
<script type="text/javascript" >
$(function () {
include("db.php");
var dataset1 = "<?php echo json_encode($dataset1); ?>";
$.plot($("#placeholder"), [ dataset1 ]);
});     

db.php :

<?php header('Content-type: text/javascript'); 
$server = "localhost";
    $user="harish";
    $password="password";  
    $database = "db";
    $connection = mysql_connect($server,$user,$password);
    $db = mysql_select_db($database,$connection);
    $query = "SELECT xval,yval FROM flottable";
    $result = mysql_query($query);        
    while($row = mysql_fetch_assoc($result))
    {
        $dataset1[] = array($row['xval'],$row['yval']);
    }
?>

参考: http://www.javascriptkit.com/javatutors/externalphp.shtml

我也用Google搜索,发现如果将其添加到.htaccess文件中,它将可以正常工作.我也尝试过,但是没有任何效果.

I also googled and found out that if I add this to an .htaccess file it would work. I tried this as well but nothing works.

有什么问题吗?

AddType application/x-httpd-php .js
AddHandler x-httpd-php5 .js
<FilesMatch "\.(js|php)$">
SetHandler application/x-httpd-php
</FilesMatch>

推荐答案

似乎您忘记设置include("db.php");之间的PHP开放括号.

Seems you forgot to set include("db.php"); between php opening brackets.

<?php include("db.php");?> 

应该可以解决问题

这篇关于在JavaScript中引用PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆