I need in my project to load some html from external file like you see in below code this my php code
if(strtolower($_SERVER["request_method"]) == "get") {
$html = "";
$path = $_GET["path"];
if($path == "cities-select-options") {
$country_code = htmlspecialchars($_GET["country_code"]);
$get_cities = get_cities($country_code);
$html .= ' <option value=""> --- </option> ';
foreach($get_cities as $city):
$html .= ' <option value="">'.$city["city_name"].'</option> ';
endforeach;
echo $html;
}elseif() {
/* */
}
}
and this my js code
$( "#ad_country" ).on("change",function() {
$country_id = $(this).val();
$.get("htmlLoader.php?path=cities-select-option&country_id="+$country_id,function(html) {
$("#ad_city").html(html);
});
});
but I'm confused by this method. because I have too many section need to be load via ajax. So my question is : there is way to do that without writing many if conditions ?
switch($_GET['path'])htmlspecialchars()? That should only be used when displaying something on a web page that isn't supposed to be rendered as HTML.echo $html;back to your JavaScript. Nest your AJAX calls.$pathvalue$path = htmlspecialchars($_GET["path"]);instead of just$path = $_GET["path"];? You never display$pathon the page, so it doesn't need to be escaped.