Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
 
Try this example link –  srk Jul 31 at 13:18
add comment

6 Answers

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>
share|improve this answer
 
no i do not want the html part to be separate. all the html part has to be displayed in the echo part only –  user1334573 Jul 31 at 13:09
 
@user1334573 updated –  Bora Jul 31 at 13:14
 
<?=$name?> it's the same. –  Maciej Czyżewski Jul 31 at 13:16
 
thanks Bora...it worked...... –  user1334573 Jul 31 at 13:17
 
@user1334573 accept this as the answer if your problem is solved –  NDM Jul 31 at 13:19
add comment
<?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>
        ';
?>
share|improve this answer
 
getting error at the $name="anurag singh"; –  user1334573 Jul 31 at 13:13
 
can you post the error message? –  TomPHP Jul 31 at 14:14
add comment

echo it into a script tag

echo '<script> var name = '.$name.';</script>';
share|improve this answer
 
sorry does not work.... –  user1334573 Jul 31 at 13:12
add comment

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>
share|improve this answer
 
i want all the html code to be inside echo '.........'; –  user1334573 Jul 31 at 13:14
add comment

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>
share|improve this answer
add comment

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

share|improve this answer
 
gives error at the line var name1=...... –  user1334573 Jul 31 at 13:15
 
@user1334573 updated...you give single and double quotes –  VIVEK-MDU Jul 31 at 13:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.