简体   繁体   English

仅当javascript语句为true时才执行php代码

[英]Execute php code only if javascript statement is true

I try to execute php code, depending on how the value of a confirm checkbox was, like in this code snippet. 我尝试执行php代码,具体取决于此代码段中确认复选框的值。

<script>
    var check = confirm ('Are you sure ?');

    if (check == true)
    {
        <?php shell_exec('sudo shutdown -r now'); ?>
    } else {
        <?php 
            header("Location: index.php"); 
            exit();
        ?>
    }

</script>

But the confirm dialog box is not showing up, and the shutdown command is executed? 但是没有显示确认对话框,并且执行了shutdown命令吗? Is this even possible? 这有可能吗?

Javascript is executed by the browser after it's send to him. Javascript在发送给他后由浏览器执行。 PHP is executed by the server before the answer is analyzed. 在分析答案之前,PHP由服务器执行。 Use an XMLHttpRequest to tell you when check is true. 使用XMLHttpRequest来告诉您何时检查为真。

You can try AJAX to send the request to php for executing the shell_exec part, for the redirect part can be managed within jquery you don't need php for that. 您可以尝试AJAX将请求发送到php以执行shell_exec部分,因为可以在jquery中管理重定向部分,而您不需要php。 But again shell_exec is VERY VERY sensitive , use it with precaution. 但是再次shell_exec 非常非常 敏感 ,请谨慎使用。

in place of header("Location: index.php"); 代替header("Location: index.php"); use window.location 使用window.location

in place of <?php shell_exec('sudo shutdown -r now'); ?> 代替<?php shell_exec('sudo shutdown -r now'); ?> <?php shell_exec('sudo shutdown -r now'); ?> use $.ajax({ code here }); <?php shell_exec('sudo shutdown -r now'); ?>使用$.ajax({ code here });

PHP is a server-side language, so you can NOT execute it from your client side JavaScript. PHP是一个服务器端语言,所以你不能从你的客户端的JavaScript执行它。 PERIOD. 期。 The dialog box is not showing up, because you have some JS errors, an the command is executed because the HTML is rendered on the server, and executes the PHP before sending the whole HTML to the Browser. 该对话框未显示,因为您有一些JS错误,由于HTML在服务器上呈现,因此执行了命令,并在将整个HTML发送到浏览器之前执行了PHP。

AND by the way, you should never do a shell_exec based on a confirm dialog like this. AND,顺便说一句,您永远不要基于这样的确认对话框来执行shell_exec

This might be an approach: 这可能是一种方法:

Part of your HTML: HTML的一部分:

<script>
    function shutDown() {
        if ( confirm ('Are you sure ?') )
        {
            var xhReq = new XMLHttpRequest();

            xhReq.open( "POST", "ajax.php", false );
            xhReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhReq.send("cmd=shutdown");

            var serverResponse = xhReq.responseText;
            alert(serverResponse); // Shows the return from the PHP script
        } else {
            window.location.href = "index.php";
        }
    }
</script>
<a href="javascript:void(0)" onclick="javascript:shutDown()"/>shutdown</a>

Part of your PHP file (ajax.php), that handles the AJAX request(s): 您的PHP文件(ajax.php)的一部分,用于处理AJAX请求:

if( isset( $_POST["cmd"] ) && $_POST["cmd"] == "shutdown" ) {
    // your shutdown code
    // I'm not sure if "sudo" really works in this context, because you have to verify with a password, except your php process is already running with sudo rights
    shell_exec('sudo shutdown -r now');
}

// you can return something here,
// but it only makes sense if you don't shutdown the server
// you are running the website to shutdown ;) 

This should work as long as the page is save as *.php 只要页面另存为* .php,它就应该起作用

<script>

    if (confirm ('Are you sure ?'))
    {
        <?php shell_exec('sudo shutdown -r now'); ?>
    } else {
        <?php 
            header("Location: index.php"); 
            exit();
        ?>
    }

</script>

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

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