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

时间:2013-07-31 13:05:06

标签: php javascript jquery html

在我发布的这段代码中,我有一个问题。我希望我的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>
    ';
?>

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

6 个答案:

答案 0 :(得分:5)

在带有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>
    ';
?>

你可以使用类似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>

答案 1 :(得分:0)

<?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>
        ';
?>

答案 2 :(得分:0)

将其回显到脚本标记

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

答案 3 :(得分:0)

你可以这样做 -

<?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>

答案 4 :(得分:0)

你传递字符串$ name不是因为你使用了'...'这让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>

答案 5 :(得分:0)

试试这个

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

您可以将此值指定为var name= "'. $name.'";