简体   繁体   English

使用Java脚本更改PHP输出

[英]Changing PHP output using Javascript

  • Im building a website that prints some data to a table from a mySQL database using PHP. 我正在建立一个网站,该网站使用PHP将一些数据从mySQL数据库打印到表中。
  • I would like this data to be refreshed with a different search query when I flip a switch. 我希望在翻转开关时可以使用其他搜索查询来刷新此数据。

Here is the javascript function for the switch: 这是开关的javascript函数:

function switch2hecked() {
    if (document.getElementById("switch2").checked)
                             // update mySQL query  }

My PHP code looks like this: 我的PHP代码如下所示:

        $sql = "SELECT * FROM $table WHERE day = 'mon' ORDER BY realtime";
        $result = mysqli_query($con, $sql);
        while($row = mysqli_fetch_array($result)) {             
            echo
            "<div class='dtime' >".$row["displaytime"]."</div>"
            ."<div class='name' >".$row["name"]."</div>"
            ."<div class='venue' >".$row["venue"]."</div>"
            ."<div class='address' >".'<a href="'.$row["loc"].'"target="_blank"> '.$row["address"].'</a>'."</div>"
            ."<div class='signup' >".$row["signup"]."</div>"
            ."<div class='email' >".'<a href="mailto:'.$row["email"].'"target="_blank"> '.$row["email"].' </a>'."</div>"
            ."<div class='details' >".$row["details"]."</div>
            <br>";
            }

All I need is some way to pass a variable into the PHP code so I can use it for an if-statement around the query. 我需要的是通过某种方式将变量传递到PHP代码中,以便可以将其用于查询的if语句。

Help much appreciated! 帮助非常感谢!

You can achieve that using AJAX. 您可以使用AJAX来实现。

AJAX allows the communication of Javascript with PHP and vice versa which updates a part of webpage without refreshing the whole page. AJAX允许Javascript与PHP进行通信,反之亦然,这可以更新网页的一部分而不刷新整个页面。 You could use AJAX with jQuery as well. 您也可以将AJAX与jQuery一起使用。 It is pretty easy to setup. 这很容易设置。

Readup this documentation which has examples too: .ajax() | 阅读此文档,其中也包含示例: .ajax()| jQuery jQuery的

Here your javascript code: 这是您的JavaScript代码:

function switch2hecked() {
    if (document.getElementById("switch2").checked) {
        document.location = 'http://mydomain/api/data?switch2=1'
    }

And your PHP code: 和您的PHP代码:

        if ($_GET['switch2']) {
            $sql = "SELECT * FROM $table WHERE day = 'mon2' ORDER BY realtime";
        } else {
            $sql = "SELECT * FROM $table WHERE day = 'mon' ORDER BY realtime";
        }
        $result = mysqli_query($con, $sql);
        while($row = mysqli_fetch_array($result)) {             
            echo
            "<div class='dtime' >".$row["displaytime"]."</div>"
            ."<div class='name' >".$row["name"]."</div>"
            ."<div class='venue' >".$row["venue"]."</div>"
            ."<div class='address' >".'<a href="'.$row["loc"].'"target="_blank"> '.$row["address"].'</a>'."</div>"
            ."<div class='signup' >".$row["signup"]."</div>"
            ."<div class='email' >".'<a href="mailto:'.$row["email"].'"target="_blank"> '.$row["email"].' </a>'."</div>"
            ."<div class='details' >".$row["details"]."</div>
            <br>";
            }

Of course, if you want to have a more dynamic page you can use AJAX but it is a bit more complicated. 当然,如果您想拥有一个更加动态的页面,则可以使用AJAX,但这会更加复杂。

Jquery Ajax is the solution. jQuery Ajax是解决方案。 Here is an example 这是一个例子

function switch2hecked() {
     var toggle = document.getElementById("switch2").checked;
     $.ajax({
       type: "GET",
       url: "path/to/your/page.php",
       dataType: "json",
       data: { enabled: toggle }
     }).done(function( json ) {
       // Do the magic here...
       // the output of the php page will be in the json variable. 
     });
}

So your PHP page will be called with a GET param. 因此,您的PHP页面将使用GET参数进行调用。

path/to/your/page.php?enabled=true

You will need to output the result of you query into a valid JSON string in the PHP page. 您需要将查询结果输出到PHP页面中的有效JSON字符串中。 Here is how you can do it: 这是您可以执行的操作:

if ($_GET['enabled']) {
    $sql = "SELECT * FROM $table WHERE day = 'mon2' ORDER BY realtime";
} else {
    $sql = "SELECT * FROM $table WHERE day = 'mon' ORDER BY realtime";
}
$result = mysqli_query($con, $sql);
echo json_encode($result);

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

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