Im using Ajax to get a value from the Drop down box and display the result .Example, Im having three types of mess in drop down box (veg,non veg and senior mess). Here is my code from Sql part:
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hostel", $con);
$sql="SELECT registration.hosteladmissionno,registration.student_name,registration.semester,messexp.billmonth,messexp.billyear,messexp.wastagecharge,exp_amount.blockexp FROM registration,messexp,blockexp WHERE registration.mess_type = '".$q."' && status_flag=1";
$result = mysql_query($sql);
echo "<table border='1' width=80%>
<tr>
<th width=5%> S.No</th>
<th width=10%>H.Admin No</th>
<th width=10%>Student Name</th>
<th width=10%>Semester</th>
<th width=10%>Billmonth</th>
<th width=10%>Bill Year</th>
<th width=10%>Wastage Charge</th>
<th width=10%>Block Amount</th>
<th width=5%>No of Days</th>
</tr>";
$i=0;
while($row = mysql_fetch_array($result))
{
$i=$i+1;
echo "<tr>";
echo "<td align=center>" .$i."</td>";
echo "<td size=10 align=center>" . $row['hosteladmissionno'] . "</td>";
echo "<td size=35 align=center>" . $row['student_name'] . "</td>";
echo "<td size=10 align=center>" . $row['semester'] . "</td>";
echo "<td size=35 align=center>" . $row['billmonth'] . "</td>";
echo "<td size=10 align=center>" . $row['billyear'] . "</td>";
echo "<td size=35 align=center>" . $row['wastagecharge'] . "</td>";
echo "<td size=35 align=center>" . $row['exp_amount'] . "</td>";
echo "<td align=center> <input type='text' name='days' size=2> </td> ";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Ajax Part:
<script type="text/javascript">
function showUser(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","messbill1.php?q="+str,true);
xmlhttp.send();
}
</script>
From this I wanna insert The displayed details while we select the mess. how do i do the insert query. The field names are from different Tables.
Forms part:
<h2 align='center'><b>Mess Bill</h1></b>
<form action="messbill.php" onchange="showUser(this.value)"/>
<center>
<table>
<?php
$link = mysql_connect("localhost", "root", "");
mysql_select_db("hostel",$link);
$query = "SELECT mess_type FROM mess_type";
$result = mysql_query($query);
print "<tr><td><b>MESS TYPE:</b></td><td><SELECT name='users' onchange='showUser(this.value)'>";
while ($line = mysql_fetch_array($result))
{
foreach ($line as $value)
{
print "<OPTION value='$value'";
}
print ">$value</OPTION>";
}
mysql_close($link);
print "</SELECT></td></tr>";
?>