dpsx99068 2012-06-22 22:21
浏览 52

PHP AJAX - 具有特定实时状态检查和操作的按钮

I need a real time loop "status check" for buttons. Buttons "class" or "id" name is going to be changed depending on value it gets from txt file. This class/id name then will be processed with CSS. Also this button class or id depending from name should trigger/call function just to run specific php file.

Before: I have done it using only PHP, but there were no realtime "status check" :< It looked like this:

<?php
if(isset($_POST['run1']))
{exec('run1.bat');}?>         // In this part it is waiting a button push with specific                 
<?php                        //  class name and then runs some bat file which runs some                   
if(isset($_POST['run2']))   //   command and writes output to R1.txt
{exec('run2.bat');}?>
<?php

$r1 = "R1.txt";                   //This part reads txt file R1.txt  ...
$fr1 = fopen($r1, "a+");         
$sizer1 = filesize($r1);
$tr1 = fread($fr1, $sizer1);
sscanf($tr1, "SOMERANDOM TEXT(%d)", $nr1);   // ...and gets value 1 or none
fclose($fr1);
?>
<form action="" method="post">   //This part is a form
<?php
if ($nr1=="1")                  //Here it check's value from txt and load specific
{                               //  type of button.
 echo '<input type="submit" class="runing1" name="run1" value="">' . "
";
}
else
{
 echo '<input type="submit" class="runing2" name="run2" value="">' . "
";
}
?>

For now i think out structure of it like this: There is gonna be check.php with something like

    <?php
    $r1 = "R1.txt";                   //This part reads txt file R1.txt  ...
    $fr1 = fopen($r1, "a+");         
    $sizer1 = filesize($r1);
    $tr1 = fread($fr1, $sizer1);
    sscanf($tr1, "SOMERANDOM TEXT(%d)", $nr1);   // ...and gets value 1 or none
    fclose($fr1);
    ?>
    <?php
    $r2 = "R2.txt";                   //This part reads txt file R2.txt  ...
    $fr2 = fopen($r2, "a+");         
    $sizer2 = filesize($r2);
    $tr2 = fread($fr2, $sizer2);
    sscanf($tr2, "SOMERANDOM TEXT(%d)", $nr2);   // ...and gets value 1 or none
    fclose($fr2);
    ?>

This PHP file should POST those $nr1 and $nr2 values some how Outside for Ajax. Then there will be index.php/htm With several buttons which runs some kind of loop thru check.php and get the value for buttons and applies specified class or id on buttons. Like yes/no or On/OFf etc. And then depending on button status class/id it should run specific function. In "Perfectness" it would be great if with help of AJAX the class or id would be perhaps variable, cuz it could be POSTed to enother run.php file with similar code...

<?php
if(isset($_POST['$class-name-from-button']))
{exec('($_POST['$class-name-from-button']).bat');}?>                          
<?php  

It could save a lot of "repeating same line code" space i think.

  • 写回答

1条回答 默认 最新

  • dongzen5577 2014-06-25 21:47
    关注

    You can use $.post, $.get or a more general $.ajax(here you specify manually if the data should be sent in post or get format ).

    I will demonstrate the $.post format.

    var url = "yourserver.com/check.php";
    var data = "checking=true";
    $.post(url, data, function(result){
    if(result == "1")
    $("input").removeClass("runing2").addClass("runing1");
    else
    $("input").removeClass("runing3").addClass("runing2");
    
    });
    

    And your check.php should contain this code:

    <?php
    if(isset($_POST["checking"])){
    $r1 = "R1.txt";                   //This part reads txt file R1.txt  ...
    $fr1 = fopen($r1, "a+");         
    $sizer1 = filesize($r1);
    $tr1 = fread($fr1, $sizer1);
    sscanf($tr1, "SOMERANDOM TEXT(%d)", $nr1);   // ...and gets value 1 or none
    fclose($fr1);
    
    echo $nr1;
    }
    ?>
    

    Here are some references:

    http://api.jquery.com/jquery.post/

    http://api.jquery.com/jquery.get/

    http://api.jquery.com/jquery.ajax/

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么