skip to Main Content

I try to pass a data code igniter controller to ajax request the written function not working

Controller

public function selectPlaygrounds() {
    $data = $this->branchatm->getCitiesFromState($state);
    $result='';
    if($data){
        foreach($data as $list){
            $result.="<li ng-click='sawAll($list->city);'>$list->city</li>";
        }
    }
    sendJsonResponse($result);  
}

Ajax call

$.ajax({
    'method': 'GET',
    'url': base_url +'Ground/selectPlaygrounds', 
    success: function(data) {
        var newData = data.replace(/"/g, "")
        if(newData == ""){
        }else{
             $("#ls-city-grnd").html(newData);
        }
    }
});

Here result show in browser network tab

<li ng-click='sawAll(North Goa);'>North Goa</li>
<li ng-click='sawAll(South Goa);'>South Goa</li>

I wand result use quotes around the name of the city inside the sawAll function like this

<li ng-click="sawAll('North Goa');">North Goa</li>

Any idea to solve this issue?

2

Answers


  1. Just replace your li in controller like this

    $result .= '<li ng-click="selectbranchcity('' . $list->city . '');">' . $list->city . '</li>';
    

    Updated Solution

    you can use the htmlspecialchars() function as following.

    public function selectPlaygrounds() {
        $data = $this->branchatm->getCitiesFromState($state);
        $result='';
        if($data){
            foreach($data as $list){
                $city = htmlspecialchars($list->city, ENT_QUOTES, 'UTF-8');
                $result.="<li ng-click="sawAll('$city');">$city</li>";
            }
        }
        $this->output
             ->set_content_type('application/json')
             ->set_output(json_encode($result));
    }
    

    Hope this will help you.

    Login or Signup to reply.
  2. To add quotes around the city name, you need to check which quote in your string needs to be escaped and which not. If this is your target string:

    <li ng-click="sawAll('North Goa');">North Goa</li>
    

    …adding it could work like this:

      $result .= "<li ng-click="sawAll('$list->city');">$list->city</li>";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search