this is my selection option database from "lecturer" table from subjects

No.  subject  credit_hour  capacity
1   (111) AAA     3           20
2   (222) BBB     4           10
3   (333) CCC     3           30

This is my option which is displaying the option using ajax which is testing1.php

<?php
$conn = mysql_connect('localhost','root','password');
mysql_select_db('lecturer');

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

?>

<html>
<head>
<script>
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","testing2.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
    <form action="" method="post">

<select name="sub" onchange="showUser(this.value)">
    <option value="">Select a subject:</option>  
<?php $result= mysql_query('SELECT * FROM subjects'); ?>
    <?php while($row= mysql_fetch_array($result)) { 
        $list=array($row['subject'],$row['credit_hour'],$row['capacity']);
        ?>

    <option value=<?php echo $row['No']?> >
          <?php echo htmlspecialchars($row['subject'] ); ?>

          <?php echo"credit hour";
          echo htmlspecialchars($row['credit_hour'] ); ?>

        <?php echo"capacity";
          echo htmlspecialchars($row['capacity'] ); ?>
        </option>
    <?php 



    } ?>
    </select>

        <input type="submit">
</form>
<br>
<div id="txtHint"><b>subject info will be listed here.</b></div>

</body>
</html>

this is the one is displaying it into table which is selected in the option testing2.php

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','password','lecturer');
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }



mysqli_select_db($con,"lecturer");
$sql="SELECT * FROM subjects WHERE No = '".$q."'";


$result = mysqli_query($con,$sql);


echo "<table border='1'>
<tr>
<th>Subject</th>
<th>Credit_hour</th>
<th>Capacity<th>

</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['subject'] . "</td>";
  echo "<td>" . $row['credit_hour'] . "</td>";
  echo "<td>" . $row['capacity'] . "</td>";
  echo "</tr>";
  }
echo "</table>";



mysqli_close($con);
?> 

i am trying to create a button which is when i selected the option which is (111)AAA credit hour 3 capacity 20 and submit it will automatically insert into a new table which is user_subject

how to make the submit button???
thank you

Recommended Answers

All 2 Replies

Why do you not use the JQuery ?

Member Avatar for diafol

If your data is static, i.e. it's almost never updated, and there's not much of it, why not store the whole lot in a json object on page load. Then you don't need to make round trips to the server every time an user selects a different lecturer. Just a thought. Not useful for DB data that's changed often though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.