简体   繁体   English

使用AJAX设置间隔

[英]Set interval with AJAX

The purpose is to display a DIV when you click on a button and then display text inside that DIV that comes from my database. 目的是当您单击按钮时显示一个DIV,然后在该DIV中显示来自我的数据库的文本。 Thing is that data in databse changes, so text inside that div also. 事实是,数据库中的数据发生了变化,因此该div中的文本也发生了变化。 I would need a setInterval... with AJAX 我需要使用AJAX的setInterval ...

I'm new in javascript and don't know the good way to go... 我是javascript的新手,不知道要怎么做...

HTML: HTML:

<div onClick="showDiv();"> click </div>

<div style="display: none;" id="div"> 
Info from database:
<span style="display: hidden;" id="data1"> DATA 1 </span>
<span style="display: hidden;" id="data2"> DATA 2 </span>
</div>

javascript: javascript:

function showDiv()
{
document.querySelector("#div").style.display = "block";
setInterval(function () {getData()}, 1000);
}

function getData()
{

$.post(
            'process.php', 
            {

            },

            function(data){

                if(data == '1'){
                     document.querySelector("#data1").style.display = "inline";
                }
                else if(data == '2'){
                     document.querySelector("#data2").style.display = "inline";
                }


            },

            'text'
         );

         return false;

}

//don't know how to just take data from database without sending by POST or GET. //不知道如何仅从数据库中获取数据而不通过POST或GET发送。

php: 的PHP:

<?php

    SELECT x FROM database

    if(x == 1)
    {echo '1';}

    else if(x == 2)
    {echo '2';}

    ?>
  1. Get data using AJAX : Learn here . 使用AJAX获取数据: 在此处学习 Your code to setInterval() is correct or you can do this : setInterval(getData,1000); 您的setInterval()代码正确无误,或者您可以执行以下操作: setInterval(getData,1000);

  2. Display data in span s : 显示span s的数据:

    document.getElementById("data1").innerHTML = "your content from database"; document.getElementById("data2").innerHTML = "your content from database";

You're not giving a lot of info so I will give you a basic example of getting data from a mySQL database with jQuery, Ajax and PHP. 您不会提供很多信息,所以我将为您提供一个使用jQuery,Ajax和PHP从mySQL数据库获取数据的基本示例。

First you need to include jQuery to the head of your document 首先,您需要将jQuery包含在文档的开头

<script src="http://code.jquery.com/jquery-latest.js"></script>

And then use Ajax 然后使用Ajax

function showDiv(){

    document.getElementById("div").style.display = "";
    setInterval(function (){ getData('something'); }, 1000);
}

jQuery.noConflict();
jQuery(document).ready(function($){

    getData = function(variable){

        var postVar = variable;
        var postVar2 = "exemple";

        $.ajax({
            type: "POST",
            url: "php/file.php",

            data:   'variable=' + postVar + "&" +
                    'variable2=' + postVar2,

            success: function(data){

                data = $.trim(data);
                var dataSplit = data.split("++==09s27d8fd350--b7d32n0-97bn235==++");

                if(dataSplit[0] == "1"){
                    document.getElementById("data1").innerHTML = dataSplit[1];
                }
                if(dataSplit[0] == "2"){
                    document.getElementById("data2").innerHTML = dataSplit[1];
                }
            }
        });

    }

});

Finally, you need to create an external php file (in this example "file.php" in the folder "php") to get the data from your database with mysqli 最后,您需要创建一个外部php文件(在本例中为“ php”文件夹中的“ file.php”),以便使用mysqli从数据库中获取数据。

<?php

// to prevent error, I check if the post variable is set and 
// if it's not only full of spaces

if(isset($_POST['variable']) && preg_replace("/\s+/", "", $_POST['variable']) != ""){

    $con = mysqli_connect("hostname", "username", "password", "database_name");

    $query = mysqli_query($con, "SELECT * FROM `table_name` WHERE `column_name` = '".$_POST['variable']."'");

    $results = array(); $row = 0;
    while($info = mysqli_fetch_array($query, MYSQLI_ASSOC)){

        $results[$row] = array();
        $results[$row]['column_name1'] = $info['column_name1'];
        $results[$row]['column_name2'] = $info['column_name2'];

        $row++;
    }

    foreach($results as $result => $data){

        echo "1" . "++==09s27d8fd350--b7d32n0-97bn235==++" .

        '<div>'.$data['column_name1'].'</div>'.
        '<div>'.$data['column_name2'].'</div>';
    }
}

?>

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM