0

I'm new Jquery and AJAX and I've really been struggling with the syntax I've been trying to use other tutorials as reference but nothing seems to work. I feel I have the right idea but syntax is wrong somewhere please help.

Here is the Ajax side

var var_numdatacheck  = <?php echo $datacheck; ?>;
  var var_numcheck = parseInt(var_numdatacheck);
 function activitycheck(){
 $.ajax({
          type: 'POST',
          url: 'feedupdate.php',
          data: {function: '3test', datacheck: var_numcheck},
         dataType: "json",
          success: function(data) {
              var json = eval('(' + data + ')');
              $('#datacheck').html(json['0']);
         var var_numcheck = parseInt(msg);


     //setTimeout('activitycheck()',1000)},

  error:function(msg) {
     console.log(msg);
  }

        });
    }   


  $(document).ready(function()  {
     activitycheck();
  });

Here is the php the AJAX calls

<?php
require "dbc.php";

$function = $_POST['function'];
$datacheck = $_POST['datacheck'];
$search="SELECT * FROM Feedtest ORDER BY id DESC";
$request = mysql_query($search);
$update= mysql_fetch_array($request);
$updateid = $update['id'];
$updatecheck = mysql_num_rows($request);
$data = array();



if ($function == $datacheck){
echo $updatecheck;
echo $datacheck;
}

if ($function == "3test" && $updatecheck > $datacheck )      {    
 $updatesearch="SELECT * FROM Feedtest WHERE id = '$updateid' ORDER BY id DESC";
$updatequery = mysql_query($updatesearch);
$data['id'] = $updateid;
while ($row = mysql_fetch_array($updatequery))

{
  ?>

 <?php $data[]= $row['First Name']; ?>
<?php
}
echo json_encode($data); 
} 
?>
</div>
</ul>
Musa
  • 96,336
  • 17
  • 118
  • 137
  • What does your development tools say? Are you getting the correct response? – DevlshOne Jul 27 '13 at 04:32
  • Your succes function is missing a closing `}`. If you took the time to look at your developer tools you should have seen a giant error about that. – Sumurai8 Jul 27 '13 at 04:41
  • 1
    Just by looking at your PHP file, your `$data` variable is going to be an array that looks like this: `(id => {value of $updateid}, 0 => the first $row['First Name'], 1 => the second $row['First Name'], and so on.... is this what you want? – DevlshOne Jul 27 '13 at 04:42
  • @DevlshOne yes this is what i want. pasted this incorrectly but the file has the } – user2444298 Jul 27 '13 at 06:40

1 Answers1

1

first of all ,always use JSON.parse(data) instead of eval.It is considereda a good practice. second thing is always try to debug your code by checking it in console or alerting.In your context,this is what is happening-:

 $.ajax({
          type: 'POST',
          url: 'feedupdate.php',
          data: {function: '3test', datacheck: var_numcheck},
         dataType: "json",
          success: function(data) {
              var data = eval('(' + data + ')');
               console.log("myData"+data)//debugging.check the pattern so that you can acces it the way you want!!!
                for(var  i=0;i< data.length;i++)
                {
                    alldata += "<li>"+data[i][0]+"<li><hr>";
                }
                $('#datacheck').html(alldata);

        });
    } 

For JSON.parse:

success: function(data) {
                  var data = JSON.parse(data);
                   console.log("myData"+data)//debugging.check the pattern so that you can acces it the way you want!!!
                    for(var  i in data)
                    {
                        alldata += "<li>"+data[i].First Name+"<li><hr>";
                    }
                    $('#datacheck').html(alldata);

            });
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87