将 PHP 变量发送到 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13731800/
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-24 12:45:55  来源:igfitidea点击:

Send PHP variable to javascript function

phpjavascriptpostget

提问by user1879926

I have a php file that generates a variable and I would like the variable to be put into a javascript function which is called by onclick on the main page. Is this possible to send from PHP to javascript?

我有一个生成变量的 php 文件,我希望将该变量放入一个 javascript 函数中,该函数由主页上的 onclick 调用。这可以从 PHP 发送到 javascript 吗?

回答by DiverseAndRemote.com

You can do the following:

您可以执行以下操作:

<script type='text/javascript'>
    document.body.onclick(function(){
        var myVariable = <?php echo(json_encode($myVariable)); ?>;
    };
</script>

回答by Hubro

Just write:

写就好了:

<script>
    var my_variable_name = "<?php echo $php_string; ?>";
</script>

Now it's available as a JavaScript variable by the name of my_variable_nameat any point below the above code.

现在,它可以作为 JavaScript 变量使用,名称my_variable_name位于上述代码下方的任何位置。

回答by Jason

Your JavaScript would have to be defined within a PHP-parsed file.

您的 JavaScript 必须在 PHP 解析的文件中定义。

For example, in index.php you could place

例如,在 index.php 你可以放置

<?php
$time = time();
?>
<script>
    document.write(<?php echo $time; ?>);
</script>

回答by username tbd

If I understand you correctly, you should be able to do something along the lines of the following:

如果我理解正确,您应该能够按照以下方式做一些事情:

function clicked() {
    var someVariable="<?php echo $phpVariable; ?>";
}

回答by crashwap

A great option is to use jQuery/AJAX. Look at these examplesand try them out on your server. In this example, in FILE1.php, note that it is passing a blank value. You can pass a value if you wish, which might look something like this (assuming javascript vars called usernameand password:

一个很好的选择是使用 jQuery/AJAX。 查看这些示例并在您的服务器上试用它们。在此示例中,在 FILE1.php 中,请注意它传递的是一个空白值。如果你愿意,你可以传递一个值,它可能看起来像这样(假设 javascript vars 调用usernamepassword

data: 'username='+username+'&password='+password,

In the FILE2.php example, you would retrieve those values like this:

在 FILE2.php 示例中,您将像这样检索这些值:

$uname = $_POST['username'];
$pword = $_POST['password'];

Then do your MySQL lookup and return the values thus:

然后进行 MySQL 查找并返回值:

echo 'You are logged in';

This would deliver the message You are logged into the success function in FILE1.php, and the message string would be stored in the variable called "data". Therefore, the alert(data);line in the success function would alert that message. Of course, you can echoanything that you like, even large amounts of HTML, such as entire table structures.

这会将消息传递You are logged in到 FILE1.php 中的成功函数,并且消息字符串将存储在名为“data”的变量中。因此,alert(data);成功函数中的行会提醒该消息。当然,您可以使用echo任何您喜欢的内容,甚至是大量的 HTML,例如整个表格结构。

Here is another good exampleto review.

这是另一个值得回顾的好例子

The approach is to create your form, and then use jQuery to detect the button press and submit the data to a secondary PHP file via AJAX. The above examples show how to do that.

方法是创建您的表单,然后使用 jQuery 检测按钮按下并通过 AJAX 将数据提交到辅助 PHP 文件。上面的例子展示了如何做到这一点。

The secondary PHP file receives the variables (if any are sent) and returns a response (whatever you choose to send). That response then appears in the Success: section of your AJAX call as "data" (in these examples).

辅助 PHP 文件接收变量(如果有的话)并返回一个响应(无论你选择发送什么)。然后该响应作为“数据”出现在您的 AJAX 调用的 Success: 部分(在这些示例中)。

The jQuery/AJAX code is javascript, so you have two options: you can place it within <script type="text/javascript"></script>tags within your main PHP document, or you can <?php include "my_javascript_stuff.js"; ?>at the bottom of your PHP document. If you are using jQuery, don't forget to include the jQuery library as in the examples given.

jQuery/AJAX 代码是 javascript,因此您有两种选择:您可以将它放在<script type="text/javascript"></script>主要 PHP 文档中的标记中,或者您可以<?php include "my_javascript_stuff.js"; ?>放在 PHP 文档的底部。如果您正在使用 jQuery,请不要忘记在给出的示例中包含 jQuery 库。

In your case, it sounds like you can pretty much mirror the first example I suggested, sending no data and receiving the response in the AJAX success function. Whatever you need to do with that data, though, you must do inside the success function. Seems a bit weird at first, but it works.

在您的情况下,听起来您几乎可以反映我建议的第一个示例,不发送数据并在 AJAX 成功函数中接收响应。但是,无论您需要对这些数据做什么,您都必须在成功函数中进行。起初看起来有点奇怪,但它确实有效。

回答by Ren

You can pass PHP values to JavaScript. The PHP will execute server side so the value will be calculated and then you can echo it to the HTML containing the javascript. The javascript will then execute in the clients browser with the value PHP calculated server-side.

您可以将 PHP 值传递给 JavaScript。PHP 将执行服务器端,因此将计算该值,然后您可以将其回显到包含 javascript 的 HTML。然后 javascript 将在客户端浏览器中执行,并使用 PHP 计算的服务器端值。

<script type="text/javascript">
    // Do something in JavaScript
    var x = <?php echo $calculatedValue; ?>;
    // etc..
</script>