【问题标题】:execute a php script on click of a html button单击 html 按钮执行 php 脚本
【发布时间】:2020-09-08 01:15:33
【问题描述】:

我有一个 PHP 代码,它做了很多事情,所以为了简单起见,我已经删除了不必要的代码。

在下面的 if 块中,我需要创建一个带有 $message 的弹出窗口,它应该有 okcancel 按钮。一旦我点击ok按钮,我需要尽可能在同一个文件中执行一些其他的php代码(或者它也可以是另一个PHP文件),如果我点击cancel按钮,那么它不应该做任何事情。

下面是我的test.php文件-

<?PHP
    // ..... some other code

    if (!isset($_SESSION['admin'])) {
        $text = "hello world";
        // create a pop up window with $message on it along with "ok" and "cancel" button
        echo "<script type='text/javascript'>confirm('$text');</script>";
    }

    // ..... some other code
?>

只要我可以对okcancel 按钮执行一些操作,我就可以使用JS modalHTML/CSS modal 任何东西。这可能吗?我试着环顾四周,但无法弄清楚如何做到这一点。

更新:

下面是我更新的hello.php 文件,我在其中使用了 Oliver 的建议。我尝试使用下面的代码,其中我有一个javascript函数secondUser,但是一旦我点击ok按钮,它就会弹出Sad :( - invalid session

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" type="text/javascript"></script>
<script>
function secondUser() {
    $.post( "ajax/test.php", function( data ) {
        if (data.success) {
            alert(data.message);
        } else {
            alert("Sad :( - invalid session");
        }
    });
}
</script>

<?PHP
    // ..... some other code

    if (!isset($_SESSION['admin'])) {
        $text = "hello world";
        // create a pop up window with $message on it along with "ok" and "cancel" button
        echo "<script type='text/javascript'>if (confirm('$text')) { secondUser(); };</script>";
    }

    // ..... some other code
?>

现在我无法弄清楚我上面的代码有什么问题?我只想在我的其他 php 文件中使用 ajax 执行我的test.php

【问题讨论】:

  • 这样您就可以进行 Ajax 调用或提交表单或导航到链接。
  • 同一文件中的代码块?可能不是,但在另一个 php 文件中你可以,只需在 confirm 上放置一个简单的 ternay,然后简单地添加一个指向它的 location.href。如果它是一个帖子,你需要一个表单或使用 post 发出一个 xmlhttprequest。
  • @epascarello 我想知道你是否可以举个例子如何做到这一点?
  • @user1950349 你的意思是三元组?它只是一个简写 if else confirm() ? redirect : ''
  • @kevin 我的意思是说如果你能在小提琴中提供一个例子3v4l.org一点点代码就可以了。

标签: javascript php html ajax button


【解决方案1】:

您可以使用 jQuery 库通过 AJAX 发出请求。首先是注册按钮。

<button id="cool-btn">My button</button>

按钮运行后,您可以将此脚本添加到 html 或 JS 文件中。

<script>
$(document).ready(function() {
    $("#cool-btn").click(function(){
        attemptRequest();
    });
});
</script>

现在您需要创建一个名为 attemptRequest 的函数,它会向您的 PHP 表单发出 AJAX 请求。

function attemptRequest() {
    $.post( "ajax/test.php", function( data ) {
      if (data.success) {
          alert(data.message);
      } else {
          alert("Sad :( - invalid session");
      }
    });
}

然后您需要在 ajax 文件夹中名为 test.php 的 PHP 脚本中进行会话检查并返回相应的数据。

 $data = new stdClass();
 $data->success = false;
 $data->message = "";
 if (isset($_SESSION['pageadmin'])) {
     $data->message = "hello world";
     $data->success = true;
 }
 
 return json_encode($data);

希望对你有帮助:)

【讨论】:

  • 嗨奥利弗,我尝试了你的代码,在源代码(检查)部分我看到以下 code 单击确定按钮时,它显示 Sad :( - invalid session 并且它没有进入 second_user.php 文件虽然它进入了secondUser() javascript 函数。我想知道我需要在小提琴中进行哪些更改,以便它进入second_user.php 文件。
  • 您好,用户(您应该更改您的名字:))您能告诉我您的文件现在是如何布局的吗?
  • 我已经创建了一个小提琴,它与我的设置相同。我只有一个 php 文件 (admin.php),而在那个 php 文件中,我拥有 (php+js) 的所有内容。我知道这不是最佳实践,但我稍后会尝试更改结构。我还用我尝试过的方法更新了问题。
  • 我还用我尝试过的细节更新了我的问题。
  • 嗨@user1950349 对于这个例子,你能相应地分开 PHP 和 JS/HTML 文件吗?一旦我们掌握了基础知识,我很乐意教你在另一个问题的一个文件中执行此操作。为迟到的回复道歉,具有讽刺意味的是,我通常在工作日使用 stackoverflow :)
猜你喜欢
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 2013-02-15
  • 2019-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多