简体   繁体   English

在从php调用的脚本中创建新变量

[英]Make new variable in script calling from php

I'm using jquery-3.3.1.min to live update and calling it to my main.php 我正在使用jquery-3.3.1.min进行实时更新并将其调用到我的main.php中

    <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
            $(document).ready(function() {
            setInterval(function () {
            $('#show').load('smoke.php')
            $('#show2').load('pids.php')
            $('#show3').load('flame.php')
            $('#show4').load('panic.php')

                }, 2000);
            });

///// I'm echo in php using //////我在php中使用echo

                $smoke_status            = $row['smoke_status'];
                $pids_status             = $row['pids_status'];
                $flame_status            = $row['flame_status'];
                $panic_status            = $row['panic_status'];
                $startdate               = $row['startdate'];
                $stopdate                = $row['stopdate'];

                echo "<tr>";
                echo "<td id='show'></td>";
                echo "<td id='show2'></td>";
                echo "<td id='show3'></td>";
                echo "<td id='show4'></td>";
                echo "<td>$startdate</td>";
                echo "<td>$stopdate</td>";
                echo "</tr>";

but now...i want to make my live update data into a variable in script how can i declare it. 但是现在...我想将实时更新数据转换为脚本中的变量,如何声明它。 i'm success calling this 我成功打电话给我

          var i = <?php echo $panic_status  ?>;

and how can i call 我该怎么打电话

             echo "<td id='show'></td>";
                echo "<td id='show2'></td>";
                echo "<td id='show3'></td>";
                echo "<td id='show4'></td>";

into new variable??plsss help and srry for the long question 进入新变量?请帮忙,回答很长的问题

Hello I do not know what you are actually doing with the code. 您好,我不知道您实际上在用代码做什么。 its so scattered that one cannot easily know where to start 它是如此分散,以至于不容易知道从哪里开始

1.) You are displaying a result and i did not see where you are trying to escape those database with htmlentities() or htmlspecialchars() functions against xss attack. 1.)您正在显示结果,但我没有看到您尝试使用htmlentities()htmlspecialchars()函数对xss攻击进行转义的地方。

2.) I do not know whether you are still using mysql_connect deprecated functions in your database query. 2.)我不知道您是否还在数据库查询中使用不推荐使用mysql_connect的函数。 if so please better move it to mysqli or PDO . 如果是这样,最好将其移至mysqliPDO

3.) i can see you calling simultaneously four php files simultaneously with 2 seconds call. 3)我可以看到你同时用2秒调用同时调用四个php文件。 What is it you are trying to achieve. 您正在尝试实现什么。 This will cause alot of issues to the server including latency, poor performance and over consumption of data. 这将导致服务器出现很多问题,包括延迟,性能低下和数据消耗过多。 If you need a real time update why don't you switch over to nodejs and socket.io. 如果您需要实时更新,为什么不切换到nodejs和socket.io。

To answer your question, I have created an ajax example to get you started 为了回答您的问题,我创建了一个ajax示例来帮助您入门

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>

         $(document).ready(function(){


         //$('#result').click(function(){

         var post1 = 'data to post if any';





        $('#loader').fadeIn(400).html('Please Wait. Data is being Loaded');


         // assuming that you want query result by posting a variable
         var datasend = "alert1="+ post1;

         $.ajax({

         type:'POST',
         url:'smoke.php',
         data:datasend,
         crossDomain: true,
         cache:false,
         success:function(msg){

         $('#loader').hide();
         $('#result').fadeIn('slow').prepend(msg);
         }

         });

         //})

         });


    </script>


<div id="loader"></div>
<div id="result"></div>

The above script will make a call to smoke.php and display any result contained there in 上面的脚本将调用smoke.php并显示其中包含的所有结果

<?php

$smoke_status = 'I am not smoking';

/* if data is to be displayed in html form,  you have to escape it with either htmlspecialchars() or htmlentities() functions to ensure
that XXS attack is not possible. you can read further on how to escape both single, double quotes with it as case may be
*/
echo htmlspecialchars($smoke_status);
?>

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

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