Write a file named <lastname>7a.htm. A table with a header row that displays all
ID: 3812093 • Letter: W
Question
Write a file named <lastname>7a.htm. A table with a header row that displays all the field names in the SALES table (sales table consists of saleid, saledate, customerid) of your database should be initially visible. This file should have a form with a text field for a user to enter a CUSTOMERID. With each keyup event, an AJAX request is made to a file named <lastname>7a.php.
<lastname>7a.php will take the customerID as a parameter, and produce a response that will consist of a JavaScript function call. If there is no customerID in the SALES table that starts with what is in the textfield, the function call should be not_there(). If there are customerIDs in the database table that start with what is in the text field, the function call should be there(), and it should have arguments that are all the fields of those records in the SALES table.
When <lastname>7a.htm receives the response, it should evaluate the function that was returned.
The not_there() function should delete all rows from the table other than the header row.
The there() function should dynamically create rows in the table to display the values.
Explanation / Answer
<lastname>7a.htm
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
/*This will replace table content with initial values when window loading*/
$.ajax({
url: "last1.php",
type: "GET",
data: "json",
success: function(result) {
if(result === "")
not_there();
else
there(result);
}
});
});
function show_customer(str) {
if (str.length == 0) {
$("#tableCont tbody").html("");
return;
}else{
$.ajax({
url: "last1.php?q="+str,
type: "GET",
data: "json",
success: function(result) {
if(result === "")
not_there();
else
there(result);
}
});
}
return false;
}
function there(content){
$("#tableCont tbody").html(content);
return false;
}
function not_there(){
$("#tableCont tbody").html("");
return false;
}
</script>
</head>
<body>
<p><b>Start typing a customer id in the input field below:</b></p>
<form>
Customer Id: <input type="text">
</form>
<table id="tableCont">
<thead>
<tr>
<th>Sale Id</th>
<th>Sale Date</th>
<th>Customer Id</th>
</tr>
</thead>
<tbody>
<!-- Content will be replaced here-->
</tbody>
</table>
</body>
</html>
<lastname>7a.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
$where = '';
if (isset($_GET['q']))
$where = " AND customerid = ".$_GET['q'];
$sql = "SELECT salesid, saledate, customerid FROM Sales WHERE true ".$where;
$result = $conn->query($sql);
$content = '';
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$content = "<tr>";
$content .= "<td>" . $row["salesid"]. "</td><td>" . $row["saledate"]. "</td><td>" . $row["customerid"]. "</td>";
$content .="</tr>";
}
} else {
// $content .= "<tr><td colspan="3">No result found</td></tr>";
$content = '';
}
$conn->close();
echo $content;
?>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.