簡體   English   中英

在 jQuery Z2705A83A5A0659CCE3458397267 調用之后將 SQL 數據返回到 JavaScript

[英]Getting SQL data back into JavaScript after jQuery ajax call

我對 ajax(通過 jQuery)和 JavaScript 很陌生。 我想要的是定期(和異步)執行一個 php 腳本來獲取一些 SQL 數據。 但是,我將在 JavaScript 圖表中呈現這些數據,因此我需要將其返回到我的 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 ,圖表沒有響應變化):

ajax.php(不工作):

$(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);    
   });       

推薦的方法是什么?

我認為您正在混淆您的概念。 PHP 僅在網絡服務器上運行。 Javascript在客戶端運行(即web瀏覽器)

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 腳本的解析值,但不包含腳本本身。

Javascript 在用戶計算機上運行,因此在 web 頁面上處理用戶交互和事件。 當您需要從服務器獲取數據時,您可以使用 Javascript 調用服務器腳本(在本例中為 php)。 這基本上就是 AJAX 的全部內容。 但通常.js包含在以 .js 結尾的文件中,這些文件往往不會被您的網絡服務器解析,除非 javascript 實際上包含在您的頁面中,但這並不是現在真正要做的事情。

我不知道你想通過混合 javascript 和 php 來做什么。 這不是 AJAX。

我建議你使用 JSON 之類的東西。 這個粗略的 php 腳本首先將你的結果編譯成 JSON,然后將 javascript Z2705A83A5A2759CCE34558A 調用。 您需要包含 JQUERY 庫並將整個 php 腳本保存為名為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:

$.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 

嗨,我認為您不能直接這樣做....如果您想在 PHP 和 JS 之間來回傳遞數據,您需要使用 xml Z80791B3AE7002CB88C246876D9FAA8F8 ..... http://www.tizag.com/ajaxTutorial/ajaxxmlhttprequest.php希望對我有所幫助......當我開始的時候它確實對我有幫助......我試圖做你正在嘗試的同樣的事情做然后我知道我正在嘗試混合服務器和客戶端腳本

你在這里做的是以下......

1 將參數列表傳遞給php

2 php 進行一些處理(在您的情況下查詢數據庫)並回顯所需的 output 變量

3 返回給 JS

如果您正在尋找直接代碼...那里是 go ....歸功於我上面提到的站點,它不是我的代碼...我真的向您推薦 go 到該站點...您將學到很多東西在短時間內....希望有幫助

<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.myForm.time.value = ajaxRequest.responseText;
        }
    }
    ajaxRequest.open("GET", "serverTime.php", true);
    ajaxRequest.send(null); 
}

//-->
</script>



<form name='myForm'>
Name: <input type='text' onChange="ajaxFunction();" name='username' /> <br />
Time: <input type='text' name='time' />
</form>
</body>
</html>

服務器時間.php

<?php
echo date("H:i:s"); 
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM