admin管理员组

文章数量:1306923

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
    $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 $name to the variable name1 than 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 $name stored in JavaScript variable name1.

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
    $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 $name to the variable name1 than 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 $name stored in JavaScript variable name1.

Share Improve this question edited Jul 31, 2013 at 13:16 Maciej A. Czyzewski 1,5291 gold badge13 silver badges24 bronze badges asked Jul 31, 2013 at 13:05 Anurag SinghAnurag Singh 7276 gold badges10 silver badges27 bronze badges 1
  • Try this example link – Sunil Kumar Commented Jul 31, 2013 at 13:18
Add a ment  | 

6 Answers 6

Reset to default 5

In javascript with echo version: 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

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

echo it into a script tag

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

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>

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.

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

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.'";

本文标签: How to assign a PHP variable value to JavaScript variableStack Overflow