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?
getJSON
means that you have to return JSON formatted data. – wared Dec 4 at 23:39