View Single Post
Old 02-22-2013, 08:28 PM   PM User | #1
kksandyrox
New to the CF scene

 
Join Date: Feb 2013
Posts: 5
Thanks: 3
Thanked 0 Times in 0 Posts
kksandyrox is an unknown quantity at this point
Passing PHP variable to Javascript using AJAX

I have a mysql table called as step1, It has some number of rows, that keeps changing time to time.

What I want is, depending on the number of rows in the table, I need to display those many number of buttons on my web page.

How do I go about it? I am new to AJAX, so detailed explanation(+code) would be appreciated.

My Php code is

Code:
<?php
$q=$_GET["q"];

$con = mysql_connect("localhost","root","");
if (!$con)
  	{
		die('Could not connect: ' . mysql_error());
  	}
mysql_select_db("test", $con);

$result = mysql_query("SELECT * FROM step1 WHERE id = '".$q."'");

$num_rows = mysql_num_rows($result);
$q=$num_rows;
?>
I believe the Number of rows is transferred to variable "q" now.

Next, my html code is

Code:
<html> 
  <body>  
 
 <div id="myDiv"></div>
 
 <script>
 function fone()
 {
 var xmlhttp;
 xmlhttp=new XMLHttpRequest();
 xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","2.php?q=",true);
xmlhttp.send();
myFunction();
}

function myFunction()
{
for(i=1;i<="2.php?q=";i++)
{
var btn=document.createElement("BUTTON");
btn.id = "join_btn" + i;
btn.onclick = function() {
var getID = parseInt(this.id.split("join_btn")[1]);
document.location.href = pages[getID-1];
}
var t=document.createTextNode("JOIN");
document.body.appendChild(btn);
btn.appendChild(t); 

document.body.appendChild(document.createElement("br"));
document.body.appendChild(document.createElement("br"));
}
}; 
</script>
<body onload="fone()">
 
 </body> 
 </html>
So depending on the number of rows, I need to display those many buttons.

Where Have i gone wrong??
kksandyrox is offline   Reply With Quote