Write the PHP code for a web page course_query.php that displays the courses a s
ID: 3731338 • Letter: W
Question
Write the PHP code for a web page course_query.php that displays the courses a student takes for this term. The page should contain a short form with a text box where the user can type a student id. The form submits back to the same page, course_query.php. Assume we use MySQL RDBMS and we have a table structure as course_student (id varchar(20), student_id varchar(20), course_name varchar(20), instructor_id varchar(2))). Display the results as a table.
Bonus (Use AJAX +1 point, Use Webservice/JSOn +1 point)
Explanation / Answer
Hello..
I have a solution for you using AJAX..
Firstly we have to create database table course_student in mysql...
mysql> create database college;
Query OK, 1 row affected (0.02 sec)
mysql> use college;
Database changed
mysql> create table course_student(id varchar(20), student_id varchar(20) primary key, course_name varchar(20), instructor_id varchar(2));
Query OK, 0 rows affected (0.08 sec)
mysql> desc course_student;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| id | varchar(20) | YES | | NULL | |
| student_id | varchar(20) | NO | PRI | NULL | |
| course_name | varchar(20) | YES | | NULL | |
| instructor_id | varchar(2) | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into course_student values('11','10001','Java,HTML,Java Script,CSS,PHP','111');
Query OK, 1 row affected, 2 warnings (0.00 sec)
mysql> insert into course_student values('12','10002','HTML,CSS,PHP','112');
Query OK, 1 row affected, 1 warning (0.02 sec)
mysql> insert into course_student values('13','10003','HTML,PHP','112');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from course_student;
+------+------------+----------------------+---------------+
| id | student_id | course_name | instructor_id |
+------+------------+----------------------+---------------+
| 11 | 10001 | Java,HTML,Java Scrip | 11 |
| 12 | 10002 | HTML,CSS,PHP | 11 |
| 13 | 10003 | HTML,PHP | 11 |
+------+------------+----------------------+---------------+
3 rows in set (0.00 sec)
Source Code
Course_Student.html
<html>
<head>
<title>Course_Student</title>
<script language="javascript">
var x=false;
if(window.XMLHttpRequest) // to check the browser type
x=new XMLHttpRequest();
else
if(window.ActiveXObject)
x=new ActiveXObject("Microsoft.XMLHTTP");
function getdata(ds,divid)
{
if(x)
{
var obj=document.getElementById(divid);
x.open("POST",ds); //seng data to the server using post method
x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
x.onreadystatechange=function() // anonymous function
{
if(x.readystate==4 && x.status==200) // checking error
obj.innerHTML=x.responseText;
}
var aa=document.f1.t1.value; // take value from text t1
x.send("t1="+aa); // send data to the server
}
}
</script> // close script
</head>
<body>
<form name="f1">
Enter Student ID <br>
<input type=text name="t1" ><br>
<input type=button value="Submit"> // call getdata function on click //operation of button
<input type=reset value="Clear">
</form>
<div id='mess'> </div> // output will be displayed here
</body>
</html>
course_query.php
<?php
if(array_key_exists('t1',$_REQUEST)) // checking the request array is empty or not
{
$x=$_REQUEST['t1']; // copying value into variable x sent from script
$con=mysql_connect('localhost','root') //connecting mysql with php script
OR die("Connection Error-- ".mysql_error());
$db=mysql_select_db('college',$con) // connection to database
OR die("Database Error-- ".mysql_error());
$query="select course_name from course_student where student_id='$x'";
$result=mysql_query($query) // execute sql query
OR die("Can not run sql query");
if($row=mysql_fetch_array($result)) // fetch result in $row and check condition is empty or not
{
echo "The Courses taking by Student ID=".$x;
echo "<BR>".$row[0]; // show value get from database table
}
else
echo "Student ID not Found";
mysql_close(); // closing database connection
}
?>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.