简体   繁体   English

点击按钮调用 PHP

[英]Call PHP on button click

I have the following button:我有以下按钮:

<input type="submit" name="kudos_button" value="★ Give kudos"/>'

And for testing purpose, I made a PHP script like this after the </html> :出于测试目的,我在</html>之后制作了一个 PHP 脚本,如下所示:

<?php
    if(isset($_POST['kudos_button'])){
        $message = "Pressed";
        echo "<script type='text/javascript'>alert('$message');</script>";
        //and then execute a sql query here
    }
?>

It all works fine, when pressing the kudos_button the message shows up.一切正常,按下kudos_button时会显示消息。 But the problem is that when the user refreshes the page, the message shows up again.但问题是当用户刷新页面时,消息又出现了。 And I get the following message:我收到以下消息:

The page that you're looking for used information that you entered.您正在查找的页面使用了您输入的信息。 Returning to that page might cause any action you took to be repeated.返回该页面可能会导致您重复执行的任何操作。 Do you want to continue?你想继续吗?

Any tips how I can avoid this, so the button does not press again when refreshing?有什么提示可以避免这种情况,因此刷新时按钮不会再次按下?

Here is my solution:这是我的解决方案:

<form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
    <input type="submit" name="kudos_button" value="★ Give kudos"/>
</form>

<?php
    if(isset($_POST['kudos_button'])){
        $message = "Pressed";
        echo "<script type='text/javascript'>alert('$message');</script>";
        //and then execute a sql query here
    }
?>

Should be worke fine.应该可以正常工作。

But you should know that PHP works regardless of the alert.但是您应该知道 PHP 无论警报如何都能正常工作。 Either way, your SQL statement will be executed after the button is pressed.无论哪种方式,您的 SQL 语句将在按下按钮后执行。

JavaScript is a Client Based Script and PHP a Server Based Script two different things. JavaScript 是基于客户端的脚本,PHP 是基于服务器的脚本是两个不同的东西。

--- EDIT --- - - 编辑 - -

I builded a Function Based PHP Script which is calling the alert and feel for 5 seconds to sleep so the alert can shown up.我构建了一个基于 Function 的 PHP 脚本,它正在调用警报并感觉 5 秒钟以休眠,以便可以显示警报。

Here is my finished solution:这是我完成的解决方案:

<form name="form1" method="POST" action="./" >
    <input type="submit" name="kudos_button" value="★ Give kudos"/>
</form>

            <?php
                if(isset($_POST['kudos_button'])){
                    session_start();
                    $_SESSION["Button_onClick"] = 1;
                    $message = "Pressed";
                    if ($_SESSION["Button_onClick"] == 1) {
                        Button_onClick();
                        $_SESSION["Button_onClick"] = 0;
                        if ($_SESSION["Button_onClick"] == 0) {
                            BackLoop();
                        }
                    }
                }
                
                Function Button_onClick(){
                    $message = "Pressed";
                    echo ("
                        <script type='text/javascript'>
                            alert('$message');
                        </script>
                    ");
                    SQL_Statement();
                }
                
                Function SQL_Statement() {
                    //and then execute a sql query here
                }
                
                Function BackLoop() {
                    session_destroy();
                    echo("
                        <script>
                            setTimeout(function () {
                                //Redirect with JavaScript
                                window.location.href= './';
                            }, 5000);
                        </script>
                    ");
                }
            ?>

Just in case for that someone have questions.以防万一有人有问题。 I added the setTimeout function so I could see the changes on the website.我添加了 setTimeout function 以便我可以在网站上看到更改。 If you don't want any infos after the alert (user case), you can delete the function.如果您在警报(用户案例)之后不想要任何信息,您可以删除 function。 You only need window.location.href= './';你只需要window.location.href= './'; . .

If you don't want to navigate to a new page, then you can't use a regular form submission.如果您不想导航到新页面,则不能使用常规表单提交。

Make the HTTP request with JavaScript instead.用 JavaScript 代替 HTTP 请求。

eg例如

const form = document.querySelector("form");
const responseHandler = response => response.json();
const dataHandler = data => {
    alert(data.message);
};
const submitHandler = event => {
    event.preventDefault()
    const data = new FormData(form);
    const url = "/path/to/dedicated/webservice/that/returns/json";
    fetch(url, { method: "POST", body: data }).then(responseHandler).then(dataHandler);
};
form.addEventListener("submit", submitHandler);

MDN has a guide on using fetch . MDN 有使用 fetch 的指南

Then your server-side code would look something like:然后您的服务器端代码将如下所示:

<?php
// Execute a sql query here

header("Content-Type: application/json");
$message = "Pressed";
echo json_encode( [ message => $message ] );

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

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