javascript
  • php
  • jquery
  • html
  • ajax
  • 2016-02-26 49 views 0 likes 
    0

    我想用我從this問題改編的腳本來顯示錶單。該腳本位於我編寫的名爲queries.js的文件中,其目的是在位於我的項目索引中的div這樣的div中打印名爲「dbMinAlert.php」的php表單的內容,我嘗試在我的索引中調用getNewData(); .php文件使用這個標籤<body onLoad="getNewData()">,但它似乎沒有做任何事情。如何在特定時間檢索帶有AJAX的PHP表單

    var data_array = ''; // this is a global variable 
    
    function getNewData() { 
        $.ajax({ 
         url: "dbMinAlert.php", 
        }) 
        .done(function(res) { 
         data_array = res; // the global variable is updated here and accessible elsewhere 
         getNewDataSuccess(); 
        }) 
        .fail(function() { 
         // handle errors here 
        }) 
        .always(function() { 
         // we've completed the call and updated the global variable, so set a timeout to make the call again 
         setTimeout(getNewData, 2000); 
        }); 
    } 
    
    function getNewDataSuccess() { 
        //console.log(data_array); 
        document.getElementById("recentExits").innerHTML=data_array; 
    } 
    getNewData();` 
    

    ---這個PHP代碼的工作原理,它實際上做我期望它做的事情。真正的問題在於JavaScript,對於我所關心的下一個PHP表單可能會打印出「Hello world」消息,但我希望它顯示在放置在索引中的div內,而不必將任何內容發佈到dbMinAlert.php。

    define("HOST", "localhost"); 
    define("DBUSER", "root"); 
    define("PASS", "password"); 
    define("DB", "mydb"); 
    
    // Database Error - User Message 
    define("DB_MSG_ERROR", 'Could not connect!<br />Please contact the site\'s administrator.'); 
    $conn = mysql_connect(HOST, DBUSER, PASS) or die(DB_MSG_ERROR); 
    $db = mysql_select_db(DB) or die(DB_MSG_ERROR); 
    
    $query = mysql_query(" 
        SELECT * 
        FROM outputs, products 
        WHERE products.idProduct=outputs.idProduct 
        ORDER BY Date DESC, Time DESC limit 5 
    "); 
    
    echo '<ul class="news">'; 
    while ($data = mysql_fetch_array($query)) { 
        $date = date_create($data['Date']); 
        $time = date_create($data['Time']); 
        echo '<li><figure><strong>'.date_format($date,'d').'</strong>'.date_format($date,'M').date_format($date,'Y').'</figure>'.$data["idProduct"]." ".$data['prodName'].'</li>'; 
    } 
    echo '</ul>'; 
    
    +4

    切勿使用mysql_ *,它PHP7被刪除。 – Drudge

    +0

    你什麼時候調用'getNewData'? – Rayon

    +0

    我在我的索引'body onLoad中調用它,但它仍然不起作用 – All

    回答

    0

    我在this問題中發現了一個解決方案,我的代碼最終結束了。 我只是通過鍵入<body onload="return getOutput();">

    的JavaScript調用在我的索引功能

     //Min-Max Alerts 
        // handles the click event for link 1, sends the query 
        function getOutput() { 
         getRequest(
          'dbMinAlert.php', // URL for the PHP file 
          drawOutput, // handle successful request 
          drawError // handle error 
        ); 
         return false; 
        } 
        // handles drawing an error message 
        function drawError() { 
         var container = document.getElementById('recentExits'); 
         container.innerHTML = 'Bummer: there was an error!'; 
        } 
        // handles the response, adds the html 
        function drawOutput(responseText) { 
         var container = document.getElementById('recentExits'); 
         container.innerHTML = responseText; 
        } 
        // helper function for cross-browser request object 
        function getRequest(url, success, error) { 
         var req = false; 
         try{ 
          // most browsers 
          req = new XMLHttpRequest(); 
         } catch (e){ 
          // IE 
          try{ 
           req = new ActiveXObject("Msxml2.XMLHTTP"); 
          } catch(e) { 
           // try an older version 
           try{ 
            req = new ActiveXObject("Microsoft.XMLHTTP"); 
           } catch(e) { 
            return false; 
           } 
          } 
         } 
         if (!req) return false; 
         if (typeof success != 'function') success = function() {}; 
         if (typeof error!= 'function') error = function() {}; 
         req.onreadystatechange = function(){ 
          if(req.readyState == 4) { 
           return req.status === 200 ? 
            success(req.responseText) : error(req.status); 
          } 
         } 
         req.open("GET", url, true); 
         req.send(null); 
         return req; 
        } 
    
    0

    您必須首次執行此功能。

    getNewData();

    +0

    我以多種方式嘗試過,但不起作用。 – All

    0

    這可能是你從php返回結果的方式。而不是做多個回聲,你可以首先分配你的結果在單個PHP變量,並最終做單回聲。

    $result = '<ul class="news">'; 
    while ($data = mysql_fetch_array($query)) { 
    $date = date_create($data['Date']); 
    $time = date_create($data['Time']); 
    $result = $result + '<li><figure><strong>'.date_format($date,'d').'</strong>'.date_format($date,'M').date_format($date,'Y').'</figure>'.$data["idProduct"]." ".$data['prodName'].'</li>';} 
    
    $result = $result + '</ul>'; 
    echo $result; 
    
    +0

    不,不是這樣的,根據我的經驗,使用ajax表單在

    標籤內整體打印,這就是我用過的所有ajax函數的樣子,無論我如何在php文件中打印內容,該方法總是對我來說正確。 – All

     相關問題

    • 暫無相關問題^_^