1

I am currently trying to draw some dynamically loaded graphs using google's visualization tools.

I want to pull the data from an sql database. I have a php script (getnumber.php) that is capable of doing so.

I am trying to use this php script within my javascript that draws the graphs.

<html>
<head>
<!--Load the AJAX API-->
  <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
  function drawChart(Z)
  {
    tmpdata = new google.visualization.DataTable();
    datalist.push(tmpdata);
    datalist[Z].addColumn('string', 'Complete');
    datalist[Z].addRows([['Finished', $.ajax({url:"getnumber.php"})], ['Incomplete', 10]]);
    .
    .
    .
  }
  window.setInterval("drawChart()", 1000);
</script>

I realise that this use of $.ajax is completely wrong but I'm stumped!

2
  • 1
    What does "is capable of doing so" mean? Does the script return some sort of JSON? Also, have you ever used $ajax in another situation then this? Commented Aug 1, 2012 at 17:06
  • No it's the first time I've used it. The php script pulls the data successfully, it's getting it to return it to my javascript that I am having an issue with. I was not returning it as JSON. I had just tried to return the php variable. Although I am now realising this probably won't work. If it's any help the variable is just an integer. Commented Aug 2, 2012 at 8:15

2 Answers 2

1

You should try using either $.post() or $.get() functions instead of using the basic ajax one. Anyways, you can manipulate the data in thsoe 3 functions this way

$.post('getnumber.php',function(data){
/* Do whatever you want with the data you grabbed from the php page. */
});

I'm not sure of what your question is exactly, but I hope this will help you.

Have a nice day!

EDIT: The functions themselves don't contain the data, it is contained into the function(data){} portion of the $.ajax() call.

Edit2: The $.ajax(); function has a parameter called success(data, textStatus, jqXHR)which can be used, compared to the two other functions.

http://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

3 Comments

Hi thank's for your help. I have now got the script to call this way. Within the function I am trying to assign the data to a variable to use elsewhere. However, nothing appears to have been assigned. function(data){variable1 = data} In what form do I need to return the variable from the php script? Currently I was just trying return $max_number but the javascript picks it up as being empty. Perhaps this return needs to be within a function? Or can it return just from the script.
Hi, first of all I would appreciate if you could mark the question as solved. Secondly, the data variable contains the content of your query result (whatever db you use, mssql,mysql,etc.) take a look at this page for a great ajax example with a php page being called: codingcereal.com/2009/09/retrieving-data-using-ajax-jquery
0

I ended up using the following:

my_url="dosomething.php";
function getvar() {
  var json = null;
  $.ajax({
      'async': false,
      'global': false,
      'url': my_url,
      'dataType': "json",
      'success': function (data) {
          json = data;
      }
  });
  json = parseInt(json);
  return json;
};

With the php script ending with:

echo json_encode($id_max);

For some reason I could not get $.post to work. Thanks for the help anyway.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.