title: Using variable from ajax response as json
body: <p>Im still newbie from ajax and Jquery, I will show my data base in graphic using c3.js, but I cant use my ajax response to javascript variable. This is my JSON response from response.php</p>
  
  <pre><code>[{"time":"2014-05-20 17:25:00","sensor1":"25","sensor2":"20","sensor3":"31","sensor4":"33","sensor5":"27"},{"time":"2014-05-20 17:26:00","sensor1":"26","sensor2":"23","sensor3":"33","sensor4":"31","sensor5":"23"}]
  </code></pre>
  
  <p>and this is my response.php</p>
  
  <pre><code>&lt;?php
  $dbhost = "localhost";
  $dbuser = "root";
  $dbpass = "";
  $dbname = "perkebunan";
  
  //Connect to MySQL Server
  $server = mysql_connect($dbhost, $dbuser, $dbpass);
  
  //Select Database
  mysql_select_db($dbname, $server) or die(mysql_error());
  //build query
  $query = "SELECT * FROM sensor";    
  //Execute query
  $qry_result = mysql_query($query) or die(mysql_error());
  $results = array();
  while($row = mysql_fetch_assoc($qry_result))
  {
      $temp = array(
          'time' =&gt; $row['time'],
          'sensor1' =&gt; $row['sensor1'],
          'sensor2' =&gt; $row['sensor2'],
          'sensor3' =&gt; $row['sensor3'],
          'sensor4' =&gt; $row['sensor4'],
          'sensor5' =&gt; $row['sensor5']
      );
      array_push($results,$temp);
  }
  echo json_encode($results);
  ?&gt;
  </code></pre>
  
  <p>and this is my database</p>
  
  <p><a href="http://i.stack.imgur.com/yB54R.png" rel="nofollow">my database</a></p>
  
  <p>so please help me to make my json response to varible in javascript. this my trial index.php to make it posible.</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;!-- Load c3.css --&gt;
      &lt;link href="c3/c3.css" rel="stylesheet" type="text/css"&gt;
      &lt;!-- Load d3.js and c3.js --&gt;
      &lt;script src="http://d3js.org/d3.v3.min.js" charset="utf-8"&gt;&lt;/script&gt;
      &lt;script src="c3/c3.js"&gt;&lt;/script&gt;
      &lt;script src="jquery-2.2.3.min.js"&gt;&lt;/script&gt;
      &lt;script src="http://code.jquery.com/jquery-1.11.0.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="chart"&gt;&lt;/div&gt;
  &lt;script language="javascript" type="text/javascript"&gt;  
  function graphAjax(){
      var response;
       $.ajax({
          type : 'POST',
          url : 'ajax-example.php',
          dataType : 'json',
          data: { },
          success: function(data){
              response=data;
          }
      });
      var chart = c3.generate({
      bindto: '#chart',
      data: {
          json: {response
              /* I hope this variable response give result like this
              time: [2014-05-20 17:25:00, 2014-05-20 17:26:00, 2014-05-20 17:27:00, 2014-05-20 17:28:00, 2014-05-20 17:29:00],
              sensor1: [25, 26, 27, 28, 29],
              sensor2: [20, 23, 22, 25, 28],
              sensor3: [31, 33, 35, 30, 33],
              sensor4: [33, 31, 28, 25, 27],
              sensor5: [27, 23, 21, 19, 18],
              */
          }
      }
      });
  }
  &lt;/script&gt;
  
     &lt;input type='button' onclick='graphAjax()' value='Start'/&gt;
  &lt;/body&gt;
  &lt;footer&gt;
  &lt;/footer&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>thank you for your time and help.</p>