javascript
  • php
  • jquery
  • html
  • ajax
  • 2016-02-26 47 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

     相关问题

    • 暂无相关问题^_^