使用javascript访问php变量

使用javascript访问php变量,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,嗨,几个小时来我一直在努力寻找解决方案,不知道你们能不能帮我一下 我的index.php如下所示 <?php include_once"connect.php";?> <!DOCTYPE HTML> <html> <head> .... <style> .... </style> <!--- AJAX ---> <script> function showData(str) {

嗨,几个小时来我一直在努力寻找解决方案,不知道你们能不能帮我一下 我的index.php如下所示

<?php include_once"connect.php";?>
<!DOCTYPE HTML>
<html>
<head>
....
<style>
    ....
</style>
<!--- AJAX --->
<script>
    function showData(str)
    {
        if (str=="")
        {
            document.getElementById("txtHint").innerHTML="";
            return;
        } 
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getData.php?q="+str,true);
        xmlhttp.send();
    }
</script>
<body>
    <form>
        <select name="users" onchange="showData(this.value)">
            <option value="">---Select Project---</option>
            <?php 
                // query projects
                $query_pr=mysql_query("SELECT * FROM GeneralInfo");
                // get project names and its corresponding revision number
                while($pr=mysql_fetch_assoc($query_pr)){
                    echo '<option value="'.$pr['pRevNum'].'">'.$pr['pName'].'</option>';
                }

            ?>

        </select>
    </form>
    <br>
    <div id="container" align="right"></div>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.4.min.js"></script>
    <script defer="defer">
    // FUTURE USE php variable within KineticJS HERE
    ....    
    </script>
</body>

....
....
函数showData(str)
{
如果(str==“”)
{
document.getElementById(“txtHint”).innerHTML=“”;
返回;
} 
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“getData.php?q=“+str,true”);
xmlhttp.send();
}
---选择项目---

//以后在KineticJS中使用php变量 ....

my代码在根据my数据库选择一个选项值后调用getData.php非常有效。我还使用ajax获取每个选项值的相应数据。下面是getData.php

<?php
    // get the data corresponding with the selected option
$q=intval($_GET['q']);

// establish connection
...
// select the database

    // query here
$query ="SELECT DISTINCT BranchInfo.bName, BranchInfo.bRevNum, GeneralInfo.pName, GeneralInfo.pRevNum,BranchItemInfo.bIName, BranchItemInfo.bIRevNum
         FROM `GeneralInfo`
         INNER JOIN `BranchInfo`
         ON GeneralInfo.bName=BranchInfo.bName 
         AND GeneralInfo.bRevNum=BranchInfo.bRevNum
         INNER JOIN `BranchItemInfo`
         ON BranchInfo.bIName=BranchItemInfo.bIName 
         AND BranchInfo.bIRevNum=BranchItemInfo.bIRevNum
         AND GeneralInfo.pRevNum= '".$q."'";

// initialize variable with the query
$result = mysqli_query($connection,$query);

echo "<table border='1'>
<tr>
<th>bName</th>
<th>bRevNum</th>
<th>bIName</th>
<th>bIRevNum</th>
<th>pName</th>
<th>pRevNum</th>
</tr>";

$my_arr=array();
// fectching data into array
while($info = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $info['bName'] . "</td>";
    echo "<td>" . $info['bRevNum'] . "</td>";
    echo "<td>" . $info['bIName'] . "</td>";
    echo "<td>" . $info['bIRevNum']."</td>";
    echo "<td>" . $info['pName'] . "</td>";
    echo "<td>" . $info['pRevNum'] . "</td>";
    echo "</tr>";
}

// close connection
...
?>

此时,我需要的所有数据都显示为页面上的表格。我想访问getData.php中的变量(例如$info['bIRevName']),并在index.php中的javascript中使用它。我在index.php中尝试了这个

<script>
        var  myvar= <?php echo $info[bIRevNum];?>";
</script>

var myvar=“;
这是行不通的,有没有足够的方法? 感谢你的帮助

非常感谢


我意识到,当我在index.php中创建一个php变量时,脚本是有效的。但是,如果我在getData.php中创建一个php变量,脚本将无法访问该变量。有什么解决方案吗?

请尝试以下方法:

<div class="hidden"><?php echo $info[bIRevNum];?></div>
或者没有jQuery

document.querySelector("div.hidden").innerText;
什么返回PHP代码?返回预期数据?

Easy peasy

<script type="text/javascript">
.....
var some_variable = <?php echo $phpVar; ?>;
.....
</script>

.....
var some_变量=;
.....
您尝试的代码中存在语法错误。假设变量中的值是数字,我建议您尝试以下操作:

<script>
        var myvar = <?php echo $info[bIRevNum];?>;
</script>

var myvar=;

如果您将变量名作为“$info”,则它将不起作用

$info
更改为
$somethingelse
。。。
希望它对你有用…

getJSON
意味着你必须返回JSON格式的数据。只需使用AJAX。请不要使用mysql。有时键入mysql,有时键入mysqli。我实际上在index.php中创建了一个php变量,调用该变量的脚本也在index.php中,它可以工作。但是如果我创建我的getData.php中的php变量,我的脚本仍然在index.php中,无法调用该变量。有什么方法可以处理这个问题吗?与我回答的“martynas”相同。如果我在getData.php中创建php变量,我在index.php中的scrip无法访问它。它必须位于同一个文件中才能访问它。有什么方法处理它吗?
<script>
        var myvar = <?php echo $info[bIRevNum];?>;
</script>