获取SQL数据返回到JavaScript的jQuery的AJAX调用后, [英] Getting SQL data back into JavaScript after jQuery ajax call

查看:121
本文介绍了获取SQL数据返回到JavaScript的jQuery的AJAX调用后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty的新的阿贾克斯(通过jQuery)和JavaScript。我想是定期(和异步)执行一个PHP脚本,获取一些SQL数据。不过,我会present在JavaScript图形这个数据,所以我需要它重新回到我的JavaScript。

I'm pretty new to ajax (via jQuery) and JavaScript. What I would like is to execute a php script periodically (and asynchronously) that fetches some SQL data. However I will present this data in a JavaScript graph, so I need it to get back into my JavaScript.

我想,推SQL数据到一个数组中的JavaScript内嵌入PHP脚本,然后简单地获取网页本身与阿贾克斯通话,但这并没有工作(尽管我可以在页面的源代码的JavaScript改为看到,图中并没有回应更改):

I tried an embedded php script inside the JavaScript that pushes the SQL data into an array, and then simply fetches the page itself with .ajax call, but this did not work (even though I could see in the page source that the JavaScript was changed, the graph did not respond to the changes):

ajax.php(不工作):

ajax.php (not working):

$(function () {        
   function fetchData() {            
   $.ajax('ajax.php');
   <?php        
   try
   {
      $now = time();
      $query = "select * from jet_pressure;"        
      $result = $db->query($query);
      foreach ($result as $row)
      {                                      
         print "d1.push([".$row['timestamp'].",".$row['unknown']."]);";            
      }                                
   }
   catch (Exception $e)
   {
      print 'Exception : '.$e->getMessage();
   }
   ?>           
   $.plot($("#placeholder"), [ d1]);
   setTimeout(fetchData, 5000);
   }    
    setTimeout(fetchData, 500);    
   });       

什么是推荐的方式做到这一点?

What is the recommended way to do this?

推荐答案

我想你混淆的概念。 PHP只能运行在Web服务器。的Javascript运行在客户端(即Web浏览器)

I think you are mixing up your concepts. PHP only runs on the webserver. Javascript runs on the client (ie the web browser)

如果您创建一个的.php 分机作为你的 ajax.php ,它会提供一个网页从你的Web服务器,一旦一切都包含这是一个在&LT; PHP&GT; 块将被服务器解析 - 它不是动态的?。

If you create a page with the .php extension as in your ajax.php, it will be served from you web server once and everything it contains that's a in the <?php ?> block will be parsed by the server - it not dynamic.

生成的页面包含你的PHP脚本解析值,而不是脚本本身。

The resultant page contains parsed values from your php script, but not the script itself.

的Javascript操作的用户的计算机上,并且因此处理该网页上的用户交互和事件。您可以使用JavaScript调用服务器脚本(在这种情况下,PHP),当你需要从服务器获取数据。这基本上是AJAX是一回事。但一般的JavaScript包含在结尾的文件的.js 往往不被您的网络服务器进行解析,除非JavaScript是实际包含在你的页面,但是这不是真的如何做这几天的事情。

Javascript operates on the users computer, and therefore handles the user interaction and events on the web page. You can use Javascript to call a server script (in this case php) when you need to get data from the server. That is basically what AJAX is all about. But generally the javascript is contained in files ending .js which tend not to be parsed by your webserver, unless the javascript is actually included in your page, but that's not really how to do things these days.

我不知道你是特林做什么用混合的JavaScript和PHP。这不是AJAX

I have no idea what you are tring to do by mixing javascript with php. This is not AJAX.

我建议你使用类似JSON。这种粗糙的PHP脚本首先将结果编制成JSON,那么JavaScript的Ajax调用。你需要有包括jQuery库和整个PHP脚本保存为名为 getdata.php

I suggest you use something like JSON. This rough php script first to compile your results into JSON, then the javascript ajax call. You'll need to have included the JQUERY library and save the whole php script as a seperate file called getdata.php.

<?php
// You'll have to do all the database select stuff here


    while ($Row = mysql_fetch_array($params)) 
{      
    $jsondata[]= array('jsobjectfield1'=>$Row["dbtablefield1"],
        'jsobjectfield2'=>$Row["dbtablefield2"],                       'jsobjectfield3'=>$Row["dbtablefield3"],                      'jsobjectfield4'=>$Row["dbtablefield4"]); 
};  
echo("{\"TableData\": ".json_encode($jsondata)."};");

?>

JavaScript的:

Javascript:

$.ajax({
    url: 'getdata.php',
    type: "POST",
    data: entereddata,
    dataType: "json",
    timeout: (7000),
    //wait 7 seconds          
    error: function(data)          
    {                     

    }, //end of ERROR handling          
    success: function(data)          
    {  
        // you'll find the data returned here:
    data.jsobjectfield1   
    }; // end of SUCCESS handling 

}); // end AJAXcall 

这篇关于获取SQL数据返回到JavaScript的jQuery的AJAX调用后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆