1

在我发布的这段代码中,我遇到了一个问题。我希望我的 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,我得到一个语法错误。请让我知道我必须做出哪些改变。这样我就得到了$name存储在 JavaScript 变量中的 PHP 变量的值name1

4

6 回答 6

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>
于 2013-07-31T13:07:15.077 回答
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>
        ';
?>
于 2013-07-31T13:08:18.403 回答
0

将其回显到脚本标签中

echo '<script> var name = '.$name.';</script>';
于 2013-07-31T13:08:28.597 回答
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>
于 2013-07-31T13:08:36.353 回答
0

您正在传递字符串 $ame 而不是变量,因为您使用了 '...' 这让 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>
于 2013-07-31T13:10:13.000 回答
0

尝试这个

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

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

于 2013-07-31T13:10:17.930 回答