-1

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?

2
  • getJSON means that you have to return JSON formatted data. Commented Dec 4, 2013 at 23:39
  • Just use AJAX. And please don't use mysql_* Sometimes you type mysql and sometimes you type mysqli. Commented Dec 4, 2013 at 23:52

3 Answers 3

0

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?

1
  • 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? Commented Dec 5, 2013 at 1:42
0

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>
1
  • 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 ? Commented Dec 5, 2013 at 1:40
0

If your are taking the variable name as "$info" it wont work.

Change $info to $somethingelse... Hope it works for you...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.