简体   繁体   English

我如何使用多个按钮通过PHP运行多种功能

[英]How do I have multiple functions available to run via PHP using multiple buttons

I managed to get my previous project working where I click a button on a webpage and it makes some text change on the webpage for all the devices that are viewing it, by making the button click run PHP code which tells all devices to run a JavaScript function, which reads the value of a text file that the PHP code is changing. 我设法使上一个项目正常运行,方法是单击网页上的一个按钮,然后通过单击运行PHP代码(告诉所有设备都运行JavaScript)来对所有正在查看该设备的设备进行一些文本更改。函数,该函数读取PHP代码正在更改的文本文件的值。

Now I want to do the same thing, but I want to have 3 different buttons that are running 3 different JavaScript functions on all the devices viewing the webpage. 现在,我想做同样的事情,但是我想拥有3个不同的按钮,它们在查看该网页的所有设备上运行3个不同的JavaScript函数。

I have tried making 3 different PHP files, and 3 different text files, and 3 different functions, but it didn't work (it seemed to think I was clicking the buttons when I wasn't), and also seemed like not a very efficient way to do it. 我试过制作3个不同的PHP文件,3个不同的文本文件和3个不同的功能,但是它没有用(似乎认为我在不使用时单击了按钮),而且看起来也不是很高效的方法。 I think it was to do with having multiple $.get() functions running at once, for all the different functions. 我认为这与同时运行多个$ .get()函数有关的所有不同功能有关。

How would I have 1 PHP file, and 1 $.get() function and make this work? 我将如何拥有1个PHP文件和1个$ .get()函数并使其工作?

Thanks, Fjpackard. 谢谢,Fjpackard。

Edit: Here is the code I tried, although I would like it if I could do it a different and more efficient way if possible. 编辑:这是我尝试过的代码,尽管如果可以的话,我希望可以使用其他更有效的方法来执行。

index.html: index.html的:

<title>PHP Test</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>

    var theButton;

    function myClick()
    {
        $.post('script.php', {});
    }

    function myClick2()
    {
        $.post('script2.php', {});
    }

    function myClick3()
    {
        $.post('script3.php', {});
    }

    function myFunc() {
        //The code you want to run on the clients
        $("#theDiv").text("Say Hello");
        setTimeout(function(){
            $("#theDiv").text("");
        },2000);
    }

    function myFunc2() {
        //The code you want to run on the clients
        $("#theDiv").text("Say Goodbye");
        setTimeout(function(){
            $("#theDiv").text("");
        },2000);
    }

    function myFunc3() {
        //The code you want to run on the clients
        $("#theDiv").text("Zip Your Mouth");
        setTimeout(function(){
            $("#theDiv").text("");
        },2000);
    }

    var lastValue = '';
    var lastValue2 = '';
    var lastValue3 = '';

        $("document").ready(function() {
        var deviceAgent = navigator.userAgent.toLowerCase();
        var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
        if (agentID) {

            // mobile code here
            $("#btn1").remove();
            $("#btn2").remove();
            $("#btn3").remove();
            setInterval(function() {
                $.get(
                    'script.php',
                    {},
                    function (data) {
                        if (data != lastValue) {
                            myFunc();
                            lastValue = data;
                        }
                    }

                );
                /*$.get(
                    'script2.php',
                    {},
                    function (data2) {
                        if (data2 != lastValue2) {
                            myFunc2();
                            lastValue = data2;
                        }
                    }

                );
                $.get(
                    'script3.php',
                    {},
                    function (data3) {
                        if (data3 != lastValue3) {
                            myFunc3();
                            lastValue = data3;
                        }
                    }

                );*/
            },100);

        }
        else {
            $("#theDiv").remove();
        }


    });

</script>
<div id="theDiv"></div>
<button id="btn1" class="btn" onclick="myClick()">Say Hello</button><br>
<button id="btn2" class="btn" onclick="myClick2()">Say Goodbye</button><br>
<button id="btn3" class="btn" onclick="myClick3()">Zip Your Mouth</button>

script.php: script.php的:

<?php
    // Disable cache
    header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
    header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header("Pragma: no-cache");

    $file = 'file.txt';

    if ($_SERVER['REQUEST_METHOD'] === 'POST')
    {
        // POST request

        $previous = file_get_contents($file);
        if ($previous === 'true')
        {
            file_put_contents($file, 'false');
        }
        else
        {
            file_put_contents($file, 'true');
        }
    }
    else
    {
        // not POST request, we assume it's GET

        echo file_get_contents($file);
    }
    exit();
?>

script2.php: script2.php:

<?php
    // Disable cache
    header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
    header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header("Pragma: no-cache");

    $file = 'file2.txt';

    if ($_SERVER['REQUEST_METHOD'] === 'POST')
    {
        // POST request

        $previous = file_get_contents($file);
        if ($previous === 'true')
        {
            file_put_contents($file, 'false');
        }
        else
        {
            file_put_contents($file, 'true');
        }
    }
    else
    {
        // not POST request, we assume it's GET

        echo file_get_contents($file);
    }
    exit();
?>

script3.php: script3.php:

<?php
    // Disable cache
    header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
    header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header("Pragma: no-cache");

    $file = 'file3.txt';

    if ($_SERVER['REQUEST_METHOD'] === 'POST')
    {
        // POST request

        $previous = file_get_contents($file);
        if ($previous === 'true')
        {
            file_put_contents($file, 'false');
        }
        else
        {
            file_put_contents($file, 'true');
        }
    }
    else
    {
        // not POST request, we assume it's GET

        echo file_get_contents($file);
    }
    exit();
?>

You could use some GET parameters and an array in the php file: 您可以在php文件中使用一些GET参数和一个array

JS code: JS代码:

function myClick()
    {
        $.post('script.php?button=1', {});
    }

    function myClick2()
    {
        $.post('script.php?button=2', {});
    }

    function myClick3()
    {
        $.post('script.php?button=3', {});
    }

And then in script.php : 然后在script.php

<?php
    session_start();
    // Disable cache
    header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
    header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0', false);
    header("Pragma: no-cache");

    $fileArray = array("1" => "file.txt", "2" => 'file2.txt', "3" => 'file3.txt');

    if(isset($_SESSION['lastFile']) && !isset($_GET['button'])){
        $file = $_SESSION['lastFile'];
    }else if(isset($_GET['button'])){
        $file = $fileArray[$_GET['button']];
    }else{
        $file = "file.txt";
    }

    $_SESSION['lastFile'] = $file;

This way, if no GET parameter is set, the script will fall back on an previous used file ( set in the session ), or else load the default ( file.txt ) 这样,如果未设置GET参数,则脚本将退回到先前使用的文件(在会话中设置),否则将加载默认文件(file.txt)

So if you do the Ajax request to script.php?button=2 , file file2.txt will be loaded, and inserted into the session variable . 因此,如果您对script.php?button=2发出Ajax请求,那么文件file2.txt将被加载并插入到session variable

Later when you request script.php ( without the button part ), the script detects that there isn't any button get variable . 稍后,当您请求script.php (不包含按钮部分)时,脚本会检测到没有任何button get variable But it knows there is an variable in the session with the previous used file, so the script will then load the previous used file. 但是它知道与先前使用的文件的会话中有一个变量,因此脚本将随后加载先前使用的文件。

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

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