0

the below function is my controller code which is called by an ajax request:

function search_featured_candidates() {       
   $skills = $this->input->post('skills');
   $this->load->model('Featured_candidate', 'featured', TRUE);
   $result = $this->featured->get_featured_candidates_by_skills($skills);
   if ($result) {
      $str = "";
      foreach ($result as $row) {
          $str .= "Name: " . $row->candidate_name . "<br/>";
          $str .= "Exp: " . $row->experience . "<br/>";
          $str .= "Skills: " . $row->skills . "<hr/>";
      }
      $html = $str;
      echo json_encode(array('html' => $html, 'success' => TRUE));
      } else {
          $html = 'No Candidates Found!';
          echo json_encode(array('html' => $html, 'success' => FALSE));
      }
 }

my view code:

<script>
$(function() {
    $("#featured_candidates").on("change paste keyup", function() {
        $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>mypage/search_featured_candidates/",
            data: {skills: $(this).val()},
            dataType: "json",
            success: function(data) {
                if (data.success === true) {
                    $("#featured").html(data.html);
                } else {
                    $("#featured").html(data.html);
                }
            }
        });
    });
});
</script>

 <div class="panel-body">
        <div>
            <input type="text" style="width: 100%" 
             name="featured_candidates" id="featured_candidates" 
             placeholder="keyword / skills" title="Featured Candidates" 
             />
            <br/><hr/>
        </div>
        <div id="featured">
            <?php
            foreach ($result as $row) {
                echo "Name: " . $row->candidate_name . "<br/>";
                echo "Exp: " . $row->experience . "<br/>";
                echo "Skills: " . $row->skills . "<hr/>";
            }
            ?>
        </div>
    </div>

now i am trying to display the result array using ajax like i have displayed in my view code using foreach. so to display it using ajax i have concatenated the array in my controller method in $str but it is not working while when i updated my controller method to this:

 function search_featured_candidates() {       
   $skills = $this->input->post('skills');
   $html = $skills ;
   echo json_encode(array('html' => $html, 'success' => TRUE));
 }

it is working fine..any help or suggesttion would be a great help...thanks in advance..

Mohammed Sufian
  • 1,743
  • 6
  • 35
  • 62

2 Answers2

0

You have a mistake here

foreach ($result as $row) {
    echo "Name: " . $row->candidate_name . <br/>";
    echo "Exp: " . $row->experience . "<br/>";
    echo "Skills: " . $row->skills . "<hr/>";
}

You forgot the "

. $row->candidate_name . "<br/>";
                    //   ^ You forgot the "
Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
0

The formulation of your question makes it difficult to know where your problem really is. But from a quick look you normally have to set proper headers in order to output json formatted data with PHP.

Try adding this before you do your echo, maybe this solves your problem:

header('Content-Type: application/json');
m.pons
  • 140
  • 1
  • 10
  • You may have a problem with your `
    ` and `
    `. Try to replace them with `
    ` and `
    `. Have a look at the link posted in this answer: http://stackoverflow.com/a/11657161/2358063
    – m.pons Mar 20 '14 at 22:58