1

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 ?

9
  • You can use switch($_GET['path']) Commented May 24, 2018 at 1:11
  • 1
    Why are you calling htmlspecialchars()? That should only be used when displaying something on a web page that isn't supposed to be rendered as HTML. Commented May 24, 2018 at 1:12
  • You never echo $html; back to your JavaScript. Nest your AJAX calls. Commented May 24, 2018 at 1:13
  • @Barmar not it should be HTML code depending on $path value Commented May 24, 2018 at 1:15
  • 1
    @joowmss My question was why $path = htmlspecialchars($_GET["path"]); instead of just $path = $_GET["path"];? You never display $path on the page, so it doesn't need to be escaped. Commented May 24, 2018 at 1:17

1 Answer 1

1

You can put the code for each path in different functions, and use an associative array to call them.

$paths = array(
    'cities-select-option' => 'get_cities_options',
    'states-select-option' => 'get_states_options',
    ...
);

function get_cities_options() {
    $html = ' <option value=""> --- </option> ';
    $country_code = $_GET["country_code"];
    $get_cities = get_cities($country_code);
    foreach($get_cities as $city):
        $html .= ' <option value="">'.$city["city_name"].'</option> ';
    endforeach;
    return $html;
}

function get_states_options() {
    ...
}

if (strtolower($_SERVER['REQUEST_METHOD'] == 'get') {
    $path = $_GET['path'];
    if (isset($paths[$path])) {
        echo $paths[$path]();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you ! I have one more question for you why you call function $var() instead using call_user_func
They're equivalent, but this syntax is simpler.
if this answer is correct please mark it as such, thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.