2013-07-31 120 views
1

在我发布的这段代码中,我有一个问题。我想我的PHP变量存储在JavaScript变量中,但它显示错误。代码如下。如何为PHP变量分配一个PHP变量值

<?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变量name1中的PHP变量$name的值。

+0

这个值试试这个例子[链接](http://p2p.wrox.com/php-faqs/11606-q如何做我传递php的变量javascript.html) –

回答

5

在有回声的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> 
+0

不,我不希望HTML部分是分开的。所有的html部分都必须在echo部分显示 –

+0

@ user1334573 updated – Bora

+0

''这是一样的。 –

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

在$ name =“anurag singh”时出错; –

+0

你可以发布错误消息吗? – TomPHP

0

呼应它插入脚本代码

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

抱歉不起作用.... –

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> 
+0

我希望所有的html代码都在echo'.........'里面; –

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

试试这个

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

您可以指定喜欢var name= "'. $name.'";

+0

给出错误在线var name1 = ...... –

+0

@ user1334573更新...你给单引号和双引号 –