I have a php and jquery code like below

<script>
$( document ).ready(function() {
  var data =[];
    $("#submit").on("click",function(){
        $.ajax({
                type:"POST",
                url:"map.php",
                success: function (html) {
                    $('#message').html(html);
                }
        });
    });
});
</script> 

PHP

<?php
define ( 'DB_HOST', 'localhost' );
define ( 'DB_USER', 'root' );
define ( 'DB_PASS', '' );
define ( 'DB_NAME', 'test' );
  $con = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
  $query = "SELECT x,y FROM app";
  $results = $con->query($query);
  $return = array();
  if($results) {
    while($row = $results->fetch_assoc()) {
      $return[] = array((float)$row['x'],(float)$row['y']);
    }
  }
   echo json_encode($return);
  $con->close();
 ?>

which return values from database like this in $('#message').html(html);

[ [ 20 , 20 ],[ 20 , 30 ],[ 50 , 35 ],[ 40 , 20 ] ]

now can you please let me know how I can parse/export/push this result into data =[]; to have an array of array there so eventually the result must look like:

var data  =[ [ 20 , 20 ],[ 20 , 30 ],[ 50 , 35 ],[ 40 , 20 ] ];

Thanks

share|improve this question
up vote 1 down vote accepted

Include dataType: 'json'

$.ajax({
     type:"POST",
     url:"map.php",
     dataType: 'json',
     success: function (html) {
         $('#message').html(html);
         data = html;
         console.log(data);
     }
});
share|improve this answer
    
Hi Romeo, Thanks for reply but is there any other way to see the actual result rather than console? because I am not sure what exactly i am getting there with some numbers like 0:, 1: – Mona Coder May 28 '14 at 6:53
    
You can see the true result if you open your Inspector / Firebug, the Network tab, then run that ajax and here in the tab you will see a new request. Click on it and on Preview you can see exactly what is sent from the server. – Romeo Onisim May 28 '14 at 7:08
    
Thanks again, just for confirmation, so the structure of the data[] in reality looks like var data =[ [ 20 , 20 ],[ 20 , 30 ],[ 50 , 35 ],[ 40 , 20 ] ]; right? I mean I am getting the correct result in console or alert(data.join('\n')); but to be honest the format of data[] is very important for me to since I am using this format to load circles to raphael.js paper so must be in that format exactly – Mona Coder May 28 '14 at 7:19
    
Yes it has to be right. You can also check what you get with the others people solution: data = JSON.parse(html); or data = $.parseJSON(html); – Romeo Onisim May 28 '14 at 7:22
    
ok thanks every one – Mona Coder May 28 '14 at 7:23

The opposite of your json_encode PHP function in Javascript is JSON.parse(json):

var myArr = JSON.parse("[ [ 20 , 20 ],[ 20 , 30 ],[ 50 , 35 ],[ 40 , 20 ] ]");
share|improve this answer
    
thanks muffel but how can I use this in my code? – Mona Coder May 28 '14 at 6:58
    
@MonaCoder instead of $('#message').html(html); write data = JSON.parse(html); – muffel May 28 '14 at 7:03

simple do

data  = $.parseJSON('[ [ 20 , 20 ],[ 20 , 30 ],[ 50 , 35 ],[ 40 , 20 ] ]');

or

data = $.parseJSON(html);

NOTE: remove var while assigning array to data variable, it will assign the array to global data variable.

So final script will be:

<script>
$( document ).ready(function() {
  var data =[];
    $("#submit").on("click",function(){
        $.ajax({
                type:"POST",
                dataType:'json',
                url:"map.php",
                success: function (html) {
                    $('#message').html(html);
                    data = $.parseJSON(html);
                }
        });
    });
});
</script>
share|improve this answer
    
hi Sankalp, thanks for code but I am getting this error "Uncaught SyntaxError: Unexpected token ," – Mona Coder May 28 '14 at 6:57
    
can you paste more details about the error you are getting? – Sankalp Bhatt May 28 '14 at 7:04

Have you tried adding - "dataType: "json" to your ajax call.. If you do this, json_encode in your php script will take care of returning proper array structure.

$.ajax({
            type:"POST",
            url:"map.php",
            dataType: "json",
            success: function (html) {
                $('#message').html(html);
            }
});
share|improve this answer
    
Hi pankaj thanks for reply now the result looks like 218.5149144,215.5990218219.2915206,216.8274247254.5833588,31‌​1.9862023254.2178971‌​,314.9889649 so how can I add them to data[]; – Mona Coder May 28 '14 at 7:01
    
can you paste the output of var_dump($return) from your php script and also check if you are receiving proper json response by using some tool like - firebug.. – pankaj May 28 '14 at 7:14

instead of echo json_encode($return); you can use echo json_encode(array('data'=>$return)); to get response in associative array format.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.