【问题标题】:PHP AJAX - Buttons with Specific Realtime Status Check and ActionPHP AJAX - 具有特定实时状态检查和操作的按钮
【发布时间】:2012-06-22 22:21:23
【问题描述】:

我需要对按钮进行实时循环“状态检查”。按钮“class”或“id”名称将根据从 txt 文件中获取的值进行更改。然后将使用 CSS 处理此类/id 名称。 此外,这个按钮类或 id 取决于名称应该触发/调用函数只是为了运行特定的 php 文件。

之前: 我只使用 PHP 完成了它,但没有实时“状态检查”:

<?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="">' . "\n";
}
else
{
 echo '<input type="submit" class="runing2" name="run2" value="">' . "\n";
}
?>

现在我认为它的结构是这样的: 会有类似的 check.php

    <?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);
    ?>

这个 PHP 文件应该 POST 那些 $nr1 和 $nr2 值在 Ajax 的外部。 然后会有index.php/htm 有几个按钮通过 check.php 运行某种循环并获取按钮的值并在按钮上应用指定的类或 id。像是/否或开/关等。 然后根据按钮状态类/ID,它应该运行特定的功能。 在“完美”中,如果在 AJAX 的帮助下类或 id 可能是可变的,那就太好了,因为它可以用类似的代码发布到另一个 run.php 文件......

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

我认为它可以节省很多“重复同一行代码”的空间。

【问题讨论】:

  • 那么问题是什么?这些批处理文件到底是做什么的?
  • 如何使用 Ajax/(无页面刷新)来完成(我仅借助 PHP 所做的)?那些 scefied 蝙蝠正在发送 SNMP 代码并将输出写入各种 txt 文件。

标签: php ajax button status


【解决方案1】:

您可以使用 $.post、$.get 或更通用的 $.ajax(您可以在此处手动指定数据是否应该以 post 或 get 格式发送)。

我将演示 $.post 格式。

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");

});

您的 check.php 应该包含以下代码:

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

以下是一些参考资料:

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

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

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

【讨论】:

    猜你喜欢
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 2017-03-08
    • 2013-09-20
    • 1970-01-01
    • 2019-09-27
    相关资源
    最近更新 更多