How can I improve this code?
jQuery:
$.ajax({
url: "get_attributes.php",
type: "post",
datatype: "json",
data: {
wtype: red_type
},
success: function (data) {
var toAppend = '';
if (typeof data === "object") {
toAppend += "<tbody>";
toAppend += "<tr></td><td class=datalabel>Type:</td><td>" + data['type'] + "</td></tr>";
toAppend += "<tr><td class=datalabel>Health:</td><td>" + data['health'] + "</td></tr>";
toAppend += "<tr><td class=datalabel>Attack:</td><td>" + data['attack'] + "</td></tr>";
toAppend += "<tr><td class=datalabel>Defense:</td><td>" + data['defense'] + "</td></tr>";
toAppend += "<tr><td class=datalabel>Speed:</td><td>" + data['speed'] + "</td></tr>";
toAppend += "<tr><td class=datalabel>Evade:</td><td>" + data['evade'] + "</td></tr>";
toAppend += "<tr><td class=datalabel>Special:</td><td>" + data['special'] + "</td></tr>";
toAppend += "</tbody>";
$("tbody").remove();
$("#red_form table").append(toAppend);
}
}
});
PHP:
<?php
header("Content-Type: application/json");
$wtype = $_POST['wtype'];
$attributes = array();
if($wtype == 'Ninja'){
$attributes['type'] = 'Ninja';
$attributes['health'] = '40-60';
$attributes['attack'] = '60-70';
$attributes['defense'] = '20-30';
$attributes['speed'] = '90-100';
$attributes['evade'] = '0.3-0.5';
$attributes['special'] = 'There is a 5% chance of 2x attack';
}
else if($wtype == 'Samurai'){
$attributes['type'] = 'Samurai';
$attributes['health'] = '60-100';
$attributes['attack'] = '75-80';
$attributes['defense'] = '35-40';
$attributes['speed'] = '60-80';
$attributes['evade'] = '0.3-0.4';
$attributes['special'] = '10% chance of restoring +10 health when evade is successful';
}
else if($wtype == 'Brawler'){
$attributes['type'] = 'Brawler';
$attributes['health'] = '90-100';
$attributes['attack'] = '65-75';
$attributes['defense'] = '40-50';
$attributes['speed'] = '40-65';
$attributes['evade'] = '0.3-0.35';
$attributes['special'] = 'increased +10 defense when health is below 20%';
}
echo json_encode($attributes);
?>