【发布时间】:2020-09-08 01:15:33
【问题描述】:
我有一个 PHP 代码,它做了很多事情,所以为了简单起见,我已经删除了不必要的代码。
在下面的 if 块中,我需要创建一个带有 $message 的弹出窗口,它应该有 ok 和 cancel 按钮。一旦我点击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
?>
只要我可以对ok 和cancel 按钮执行一些操作,我就可以使用JS modal 或HTML/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