Homework Help Homework 4: Server-side Scripting 1. objectives Get experience wit
ID: 3816982 • Letter: H
Question
Homework Help
Homework 4: Server-side Scripting 1. objectives Get experience with the PHP programming language Get experience with the Markit on Demand APIs Get experience using XML and JSON parsers in PHP 2. Description In this exercise, you are asked to create a webpage that allows you to search for stock information using the Markit on Demand APIs, and the results will be displayed in tabular format. 2.1. Description of the Search Form A user first opens a page, called stock.php(or any valid web page name where he/she can enter a company name or symbol. An example is shown in Figure 1. Providing a value for the "Company Name or Symbol" field is mandatory (the validation should be done using one of the attributes provided by HTML5). Also, the form should include a Markit on Demand disclaimer "e.g Powered by Markit on Demand linking to the web page http://www.markit.com/product/markit-on-demand Stock Search Company Name or Symbol: Search clear Powered by Markit on Demand Figure 1: Initial Search Screen The search form has two buttons: Search button: If the user clicks on the search button without providing a value in the "Company Name or Symbol" field, HTML5 should validate it (an example is shown in Figure 2). An example of valid input is shown in Figure 3. Once the user has provided valid data, your client script should send a request to your web server for stock.php with the form data. You can use either GET or POST to transfer the form data to the web server. A PHP script will retrieve the data and send it to the Markit on Demand restful Web Service. Clear button: This button must clear the result area and the text field. The Clear operation is done using a JavaScript functionExplanation / Answer
stock.php
<!-- stock.php -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Stock Search with Market Data APIs</title>
<style>
#mainbox {
width: 400px;
margin: 0 auto;
border: 2px solid black;
padding: 10px;
line-height: 20pt;
}
#output_box {
margin: 0 auto;
width: 700px;
margin-top: 10px;
text-align: left;
border: 1px solid black;
}
#empty_box {
width: 800px;
margin: 0 auto;
border: 1px solid black;
padding: 5px;
line-height: 10pt;
margin-top: 10px;
}
#website, #button_group, h1 {
text-align: center;
}
</style>
</head>
<body>
<?php
$name = "";
$companyName = "";
$companySymbol = "";
$companyExchange = "";
?>
<h1>Stock Search</h1>
<div id="mainbox">
<form id="myForm" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Company Name or Symbol: <input type="text" name="name" value="<?php if(isset($_POST['name'])) { echo htmlentities ($_POST['name']); }?>"><br/>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
}
?>
<div id="button_group">
<button type="submit" value="Submit">Search</input>
<button type="reset" value="Reset">Clear</button>
<br/>
</div>
<div id="website">
<a href="http://www.markit.com/product/markit-on-demand">Powered by Market on Demand</a>
</div>
</form>
</div>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// API part
$apiURL = "http://dev.markitondemand.com/MODApis/Api/v2/Lookup/xml?input=";
$name = urlencode($name);
$apiURL = $apiURL . $name;
// echo $apiURL;
// http://dev.markitondemand.com/MODApis/Api/v2/Lookup/xml?input=APPL
// $xmlContent will give a good output
$xmlFile = simplexml_load_file($apiURL);
// print_r($xmlFile);
if ($xmlFile -> LookupResult -> count() > 0) {
global $companyName, $companySymbol, $companyExchange;
echo "<table id="output_box" border="1" frame="void" rules="all">";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Symbol</th>";
echo "<th>Exchange</th>";
echo "<th>Details</th>";
echo "</tr>";
// read elements from xmlFile
foreach ($xmlFile -> LookupResult as $LookupResult) {
$companyName = $LookupResult -> Name;
echo "<tr><td>" . $companyName . "</td>";
$companySymbol = $LookupResult -> Symbol;
echo "<td>" . $companySymbol . "</td>";
$companyExchange = $LookupResult -> Exchange;
echo "<td>" . $companyExchange . "</td>";
echo "<td><a href="">More Info</a></td></tr>";
echo "<tr></tr>";
}
echo "</table>";
}
else {
echo "<div id="empty_box">";
echo "<h4 align='center'>No Records has been found.</h4>";
echo "</div>";
}
// the URL to JSON
$infoURL = 'http://dev.markitondemand.com/MODApis/Api/v2/Quote/json?symbol=' . $companySymbol;
echo $infoURL;
}
?>
<script>
function formReset() {
var el1 = document.getElementById("output_box");
var el2 = document.getElementById("empty_box");
el1.parentNode.removeChild( el1 );
el2.parentNode.removeChild( el2 );
}
function divRemove() {
window.location.href = "stock.php";
}
function validateForm() {
var x = document.forms["myForm"]["name"].value;
if (x == null || x == "") {
alert("Please Enter Name or Symbol!");
return false;
}
}
</script>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.