Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi I struggling to find the solution for hours and wonder if you guys could help me out I have index.php that look like this

<?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>

The my code work great to calling getData.php after select one of the option value according to the my database. Also I using ajax to get the corresponding data of each option value. Below is the 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
...
?>

At this point all the data that I need is display as a table on my page. I want to access the variables in getData.php (e.g $info['bIRevName']) and use it in a javascript within my index.php. I tried this in my index.php

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

and this is not working. Is there sufficient way to do that ? Appreciate all your helps !!

Thanks a lot !!

I realized that when I create a php variable within my index.php, the script is works. But if I create a php variable within getData.php, the script could not access that variable. Is there any solution for this?

share|improve this question
 
getJSON means that you have to return JSON formatted data. –  wared Dec 4 at 23:39
 
Just use AJAX. And please don't use mysql_* Sometimes you type mysql and sometimes you type mysqli. –  Jonast92 Dec 4 at 23:52
add comment

2 Answers

Try something like this:

<div class="hidden"><?php echo $info[bIRevNum];?></div>

And then take info with:

$("div.hidden").text();

Or without jQuery...

document.querySelector("div.hidden").innerText;

What returns that PHP code? returns the expected data?

share|improve this answer
 
same thing as I replied to 'martynas'. if I create php variable within my getData.php. My scrip in index.php could not access it. It has to be in the same file in order to access it. Is there any way to handle it? –  user3063604 Dec 5 at 1:42
add comment

Easy peasy

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

There is a syntax error in your code which you tried. Assuming the value held in the variable is a number, I suggest you try this:

<script>
        var myvar = <?php echo $info[bIRevNum];?>;
</script>
share|improve this answer
 
I have actually created a php variable within my index.php, the script to calling that variable is also in index.php, and it works. However if I create the php variable in my getData.php, my script still in index.php and can not call that variable. Is there any way to handle this ? –  user3063604 Dec 5 at 1:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.