how to tertive table data from drop down menu

11bit065
03:17:15
here i am creation g 2 drop down menu and trying to retrieve table data from second drop down menu.but i am not getting output.
question1.php(for first drop dowmenu)
<html>
<head>
<script>
function showsubcat(dept_id)
{
if (dept_id=="") {
document.getElementById("demo").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("demo").innerHTML=xmlhttp.responseText;
}
}

xmlhttp.open("GET","subid2.php?q="+dept_id,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = mysql_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("Connection failed: " . mysql_error());
}
//echo "Connected successfully";
mysql_select_db("demo",$conn);

echo"<form>";
// Form a query to populate the combo-box
$query = "SELECT DISTINCT dept_id,deptname FROM department;";

// Successful query?
if($result = mysql_query($query)) {

// If there are results returned, prepare combo-box
if($success = mysql_num_rows($result) > 0) {
// Start combo-box
echo "<b>select department<select name='department' onchange='showsubcat(this.value)'>\n";
echo "<option>-- Department --</option>\n";

// For each item in the results...
while ($row = mysql_fetch_array($result))
// Add a new option to the combo-box
echo "<option value='$row[dept_id]'>$row[deptname]</option>\n";
// End the combo-box
echo "</select>\n";
}
// No results found in the database
else { echo "No results found."; }
}

echo"<p id='demo'>this data</p>";
echo"</form>";

?>
</body>
</html>

subid2.php
<html>
<head>
<script>
function show1(cat_id)
{
//document.getElementById("demo1").innerHTML="hello world";
if (cat_id=="") {
document.getElementById("demo1").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("demo1").innerHTML=xmlhttp.responseText;
}
}

xmlhttp.open("GET","qus.php?q="+cat_id,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
//$q = 2;
$q = intval($_GET['q']);
echo"value of depid is $q";
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = mysql_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("Connection failed: " . mysql_error());
}
//echo "Connected successfully";
mysql_select_db("demo",$conn);
echo"<form>";
$sql="SELECT DISTINCT cat_id,name from category where dept_id='".$q."'";
$result=mysql_query($sql,$conn);
echo "<br>select subdepartment<select name='subdepartment' id='s1' onchange='show1(this.value)'>\n";
echo "<option>-- subDepartment --</option>\n";
while ($row = mysql_fetch_array($result))
// Add a new option to the combo-box
echo "<option value='$row[cat_id]'>$row[name]</option>\n";
// End the combo-box
echo "</select>\n";
echo"<p id='demo1'>this data</p>";
echo"</form>";
mysql_close($conn);
?>
</body>
</html>
[code]
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}

table, td, th {
border: 1px solid black;
padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
//$q=5;
$q1 = intval($_GET['q']);
echo"value of index is $q1";
$servername ="localhost";
$username ="root";
$password ="";

// Create connection
$conn = mysql_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("Connection failed: " . mysql_error());
}
echo "Connected successfully";
mysql_select_db("demo",$conn);
$sql="SELECT * FROM question WHERE cat_id = '".$q1."'";
$result=mysql_query($sql,$conn);
echo"result is $result";
echo"<table>
<th>question</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
echo"<tr>";
echo "<td>" . $row['question'] . "</td>";
echo "</tr>";
}
echo"</table>";
mysql_close($conn);
?>
</body>
</html>

[/code]
smo1234
05-03-2015
Try to print error message and see what is wrong, To get error message use mysql_error() function.

What I am seeing here is one dropdown only ? It is also recommended to use PDO for database handling.
Please Login to post your reply or start a new topic