0

I'm trying to load applicants info that i get from database (fisrt name, lasta name, ID number) in a slidetoggle that appears after clicking "Display applicants" button.

My code keeps showing ERROR DETECTED message, after I click the button "display applicants" an slidetoggle should appear and the json data be shown on the slide. can someone give me some directions about what i'm doing wrong here.

Query:

  if(isset($_GET['id'])){
   $id_oferta = $_GET['id'];
   $sql ="SELECT postulacion.* FROM postulacion WHERE id_oferta = '". $id_oferta ."'";
   $listapostulantes = mysql_query($sql) or die(mysql_error());

   $return_arr= array(); 
   $num = mysql_num_rows($listapostulantes);
     if($num > 0){
        while($row = mysql_fetch_array($listapostulantes, MYSQL_ASSOC)){

          $return_arr[] = $row;

         }
        echo json_encode($return_arr);   
     }  
  }

Script:

 $(document).ready(function(){

       $('.myslide').hide();
       $(".postulantes").on('click', function(e){

         e.preventDefault();  
         if($(this).parent().next().css('display') == 'none'){

           $('.myslide').hide('fast');
           $(this).parent().next().slideToggle('slow');

            var link = $(this).attr('href').split('&');
            var idd= link[1].match(/id=([0-9]+)/)[1]; 

          $.ajax({
                url: link[0],  
                type: 'GET',                   
                dataType:'json',
                data:{'id': idd},

                success:function(data){

                     // console.log(); 
                     var htmlStr = '';
                     $.each(data, function(key, value){

                         htmlStr += '<p>' +  value.rut_usuario + '</p>'; 
                     }); 
                     $(".myslide").html(htmlStr);                        
                 },
                error: function(){

                  $("#listaofertas").html("ERROR DETECTED");
                  //console.log();      
                } 

          });
         } 
        });
     });    

json

json array

applicants display

  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Oct 29 '14 at 16:55

2 Answers2

0

Your response, as shown under the Respuesta tab, includes not only JSON, but some HTML as well. Get rid of the html, and make sure only JSON is returned, and then jquery should execute your success callback

foxygen
  • 1,368
  • 1
  • 11
  • 22
0

Use this to split the response and "cut out" the html part.

$splitted = explode("<!DOCTYPE HTML>",json_encode($return_arr));   
echo $splitted[0];
umlal
  • 9
  • 2