如何将 PHP 变量值分配给 JavaScript 变量

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

How to assign a PHP variable value to JavaScript variable

phpjavascriptjqueryhtml

提问by Anurag Singh

In this code which I am posting i have one problem. I want my PHP variable to be stored in JavaScript variable but it shows error. The code is below.

在我发布的这段代码中,我遇到了一个问题。我希望我的 PHP 变量存储在 JavaScript 变量中,但它显示错误。代码如下。

<?php
    $name="anurag singh";

    echo '
        <html>
            <head>
            <script type="text/javascript" src="jquery-2.0.2.js"></script>
            <script type="text/javascript">
                $(document).ready(function(){
                    var name1=.$name.";"
                    $.post("main_page.php",{input1: name1}, function(msg){
                        alert("hi "+msg);
                    });
                });
            </script>
            </head>

            <body>
                <h1>This is the demo!</h1>
                <h2>In echo we can add more than one statements</h2>
            </body>
        </html>
    ';
?>

Now when I am assigning $nameto the variable name1than I get an syntax error. Please let me know what changes I have to make. So that I get the value of the PHP variable $namestored in JavaScript variable name1.

现在,当我分配$name给变量时name1,会出现语法错误。请让我知道我必须做出哪些改变。这样我就可以获取$name存储在 JavaScript 变量中的 PHP 变量的值name1

回答by Bora

In javascript with echo version: var name1= "'.$name.'";

在带有 echo 版本的 javascript 中: var name1= "'.$name.'";

<?php
$name = "anurag singh";
echo '
    <html>
        <head>
        <script type="text/javascript" src="jquery-2.0.2.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                var name1= "'.$name.'";
                $.post("main_page.php",{input1: name1}, function(msg){
                    alert("hi "+msg);
                });
            });
        </script>
        </head>

        <body>
            <h1>This is the demo!</h1>
            <h2>In echo we can add more than one statements</h2>
        </body>
    </html>
    ';
?>

And you can use like var name1= "<?php echo $name; ?>";seperated html and php

你可以使用像var name1= "<?php echo $name; ?>";分离的 html 和 php

<?php
   $name="anurag singh";
?>

<html>
    <head>
    <script type="text/javascript" src="jquery-2.0.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var name1= "<?php echo $name; ?>";
            $.post("main_page.php",{input1: name1}, function(msg){
                alert("hi "+msg);
            });
        });
    </script>
    </head>

    <body>
        <h1>This is the demo!</h1>
        <h2>In echo we can add more than one statements</h2>
    </body>
</html>

回答by DonOfDen

<?php
$name="anurag singh";
echo '
<html>
    <head>
    <script type="text/javascript" src="jquery-2.0.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var name1='.$name.'";"
            $.post("main_page.php",{input1: name1}, function(msg){
                alert("hi "+msg);
            });
        });
    </script>
    </head>

    <body>
        <h1>This is the demo!</h1>
        <h2>In echo we can add more than one statements</h2>
    </body>
</html>
        ';
?>

回答by Splendiferous

echo it into a script tag

将其回显到脚本标记中

echo '<script> var name = '.$name.';</script>';

回答by Mohammad Adil

You can do it this way -

你可以这样做 -

<?php $name="anurag singh"; ?>
<html>
    <head>
    <script type="text/javascript" src="jquery-2.0.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var name1="<?php echo $name ?>";
            $.post("main_page.php",{input1: name1}, function(msg){
                alert("hi "+msg);
            });
        });
    </script>
    </head>

    <body>
        <h1>This is the demo!</h1>
        <h2>In echo we can add more than one statements</h2>
    </body>
</html>

回答by Ahmed Habib

You are pasing string $ame not variable as because you have used '...' this let the php know that its string no more variables inside this string.

您正在传递字符串 $ame not variable 因为您使用了 '...' 这让 php 知道它的字符串在这个字符串中没有更多的变量。

<?php
$name="anurag singh";
?>

<html>
    <head>
    <script type="text/javascript" src="jquery-2.0.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var name1=<?pgp echo $name ?>;
            $.post("main_page.php",{input1: name1}, function(msg){
                alert("hi "+msg);
            });
        });
    </script>
    </head>

    <body>
        <h1>This is the demo!</h1>
        <h2>In echo we can add more than one statements</h2>
    </body>
</html>

回答by VIVEK-MDU

try this

试试这个

  $(document).ready(function(){
        var name1="'.$name.'";
        $.post("main_page.php",{input1: name1}, function(msg){
            alert("hi "+msg);
        });

you can assign this value like var name= "'. $name.'";

你可以像这样分配这个值 var name= "'. $name.'";