2013-07-31 164 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更新...你給單引號和雙引號 –