How do I write this code using PHP? <!DOCTYPE html> <html> <head> <script type=\
ID: 3720124 • Letter: H
Question
How do I write this code using PHP?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function ajax_get_json(){
var hr = new XMLHttpRequest();
hr.open("GET", "Emp_Info.json", true);
hr.setRequestHeader("Content-type", "application/json", true);
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var data = JSON.parse(hr.responseText);
var results = document.getElementById("results");
// Getting results from one employee
results.innerHTML = data.u2.name + " gets paid " + data.u2.pay + " per hour as a " + data.u2.job + " she is currently insured by " + data.u2.medical + "<br>"
+ "the medical deduction is " + data.u2.deducmed + " 401k deduction is " + data.u2.deduc401k + " stock option deduction is " + data.u2.deducstock;
}
}
hr.send(null);
document.getElementById("status").innerHTML = "requesting...";
}
</script>
</head>
<body>
<div id="results"></div>
<script type="text/javascript">ajax_get_json();</script>
</body>
</html>
And this is my json...{
"u1":{"name": "Deanna Medeiros", "pay": 50, "job": "Programmer",
"medical": "United HealthCare", "deducmed": 200, "deduc401k": 300,
"deducstock": 400, "personalEmail": "deannamedeiros72@.com",
"workEmail": "dmedeiros@aaanortheast.com"},
"u2":{"name": "Ron Rogers", "pay": 35, "job": "HVAC",
"medical": "Blue Cross & Blue Shield", "deducmed": 150, "deduc401k": 200,
"deducstock": 50, "personalEmail": "rrogers@rge.com",
"workEmail": "rrogers@rge.com"},
"u3":{"name": "John Doe", "pay": 15, "job": "Store Clerk",
"medical": "Blue Cross & Blue Shield", "deducmed": 50, "deduc401k": 25,
"deducstock": 10, "personalEmail": "jdoe@rgel.com",
"workEmail": "jdoe@aol.com"}
}
Explanation / Answer
Save below code using chegg_php.php in wamp servers www folder. Open browser and type
localhost /chegg_php.php
<?php
$json_string = 'Emp_Info.json';
//read the json content and convert it into php using decode()
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
//display required information
echo $obj['u2']['name']."gets paid " .$obj['u2']['pay']." per hour as a " .$obj['u2']['job'] ." she is currently insured by " . $obj['u2']['medical'] . "<br>"
. "the medical deduction is " . $obj['u2']['deducmed'] .
" 401k deduction is " . $obj['u2']['deduc401k'] . " stock option deduction is " .
$obj['u2']['deducstock'];;
?>
output is
Ron Rogers gets paid 35 per hour as a HVAC she is currently insured by Blue Cross & Blue Shield
the medical deduction is 150 401k deduction is 200 stock option deduction is 50
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.