Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在特定时间段使用AJAX检索php表单_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript 如何在特定时间段使用AJAX检索php表单

Javascript 如何在特定时间段使用AJAX检索php表单,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我想显示一个表单,其中包含根据问题改编的脚本。脚本位于我编写的一个名为querys.js的文件中,其目的是在项目索引中的一个div中打印名为“dbMinAlert.php”的php表单的内容,我尝试调用getNewData()在我的index.php文件中使用此标记,但它似乎什么都没做 var data_array = ''; // this is a global variable function getNewData() { $.ajax({ url: "dbM

我想显示一个表单,其中包含根据问题改编的脚本。脚本位于我编写的一个名为querys.js的文件中,其目的是在项目索引中的一个div中打印名为“dbMinAlert.php”的php表单的内容,我尝试调用
getNewData()
在我的index.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>';
定义(“主机”、“本地主机”);
定义(“DBUSER”、“root”);
定义(“通行证”、“密码”);
定义(“DB”、“mydb”);
//数据库错误-用户消息
定义(“DB_MSG_ERROR”,“无法连接!
请与站点管理员联系。”); $conn=mysql\u connect(主机、DBUSER、PASS)或die(DB\u MSG\u错误); $db=mysql\u选择\u db(db)或死亡(db\u MSG\u错误); $query=mysql\u查询(“ 挑选* 从产出、产品 其中products.idProduct=outputs.idProduct 订单按日期描述,时间描述限制5 "); 回声“
    ”; while($data=mysql\u fetch\u数组($query)){ $date=date\u创建($data['date']); $time=date\u create($data['time']); 回显“
  • ”.date_格式($date,'d')。”.date_格式($date,'M').date_格式($date,'Y')。$data['idProduct]”.“$data['prodName'.
  • ”; } 回声“
”;
您必须第一次执行该功能


getNewData()

这可能是您从php返回结果的方式。您可以先在单个php变量中指定结果,然后再执行单个echo,而不是执行多个echo

$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;
$result='
    ; while($data=mysql\u fetch\u数组($query)){ $date=date\u创建($data['date']); $time=date\u create($data['time']); $result=$result+'
  • '.date_格式($date,'d')。'.date_格式($date,'M').date_格式($date,'Y')。$data[“idProduct”]。$data['prodName'.
  • '} $result=$result+'
'; 回声$结果;
我找到了一个有问题的解决方案,我的代码就是这样结束的。 我只需在索引中键入

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;
    }

不要使用mysql,它在PHP7中被删除。你什么时候调用
getNewData
?我在我的索引“body onLoad”中调用它,但它仍然不起作用。如果有控制台错误,你可以发布控制台错误。也可能help@All检查PDO或myslqi。我尝试了多种不同的方法,但都不起作用。不,不是这样,根据我使用ajax的经验,表单作为一个整体打印在标记中,这就是我使用的所有ajax函数的情况,无论我如何打印php文件中的内容,那种方法对我来说总是正确的。