在 javascript 中执行 php 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5227132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:15:48  来源:igfitidea点击:

Executing a php script in javascript?

phpjavascriptmysqlvariablesonbeforeunload

提问by brybam

I'm trying to run a quick php script when users leave my website, and also pass a variable from my javascript to php but i'm not quite sure how to include the php file and pass it a var. But it's not actually running the php script. any ideas?

当用户离开我的网站时,我试图运行一个快速的 php 脚本,并将一个变量从我的 javascript 传递给 php,但我不太确定如何包含 php 文件并将其传递给一个 var。但它实际上并没有运行 php 脚本。有任何想法吗?

(the javascript gets the username from an external activity function, and i know that works i tested the var on an alert and its there.)

(javascript 从外部活动函数获取用户名,我知道我在警报中测试了 var 并在那里进行了测试。)

My JavaScript:

我的 JavaScript:

<script language="javascript" type="text/javascript">

            var username = null;

            function GetUsername(usernameff)
            {
                username = usernameff;
            }

            window.onbeforeunload = function () 
            {

            if (username != null)
            {

            <?php include("scripts/RemoveUserOnDisconnect.php?username=username");?>
            }

            }


            </script>

My RemoveUserOnDisconnect.php File:

我的 RemoveUserOnDisconnect.php 文件:

<?php
    mysql_connect("mysql.mysql.com", "username", "password");
    mysql_select_db("my_db");
    mysql_query("DELETE FROM my_table WHERE username = '$username'");         
?>

回答by Kyle

Try an ajax request. Depending on your php script you will need $.postor $.get

尝试 ajax 请求。根据您的 php 脚本,您将需要$.post$.get

jQuery:

jQuery:

<script language="javascript" type="text/javascript">

    var username = null;

    function GetUsername(usernameff){
        username = usernameff;
    }

    window.onbeforeunload = function(){
        if (username != null){
          $.post('scripts/RemoveUserOnDisconnect.php', {username: username}, function(){
            //successful ajax request
          }).error(function(){
            alert('error... ohh no!');
          });

        }
    }
 </script>

EDIT:

编辑:

Your php script should reference the $_POSTarray if you use my code above.

$_POST如果您使用我上面的代码,您的 php 脚本应该引用该数组。

<?php
    $username = $_POST['username'];

    mysql_connect("mysql.mysql.com", "username", "password");
    mysql_select_db("my_db");
    mysql_query("DELETE FROM my_table WHERE username = '$username'");         
?>

回答by Zoidberg

Make an ajax call to scripts/RemoveUserOnDisconnect.php?username=username. You cannot include PHP with Javascript, Javascript executes on the browser, PHP executes on the server..

对scripts/RemoveUserOnDisconnect.php?username=username 进行ajax 调用。你不能在 Javascript 中包含 PHP,Javascript 在浏览器上执行,PHP 在服务器上执行..

回答by Munim

You need to make an AJAX call to the PHP script. Your javascript code will just make a background request to the php script which will execute those MySQL statements. Google up AJAX. Recommended: Learn jQuery and use ajax using $.get()or $.post()

您需要对 PHP 脚本进行 AJAX 调用。您的 javascript 代码只会向将执行这些 MySQL 语句的 php 脚本发出后台请求。谷歌搜索 AJAX。推荐:学习jQuery并使用ajax使用$.get()$.post()