2014-03-05 41 views
-7
<script type="text/javascript"> 
    var jvalue = 'this is javascript value'; 
    <?php $abc = "<script>document.write(jvalue)</script>" ?> 
</script> 

<?php echo 'php_'.$abc; ?> 

<script type="text/javascript"> 
    var test="<?php echo $abc; ?>"; 
</script> 

<?php 
    echo '<script language="javascript">'; 
    echo 'alert(test)'; 
    echo '</script>'; 
?> 

如何提醒測試變量,它包含PHP的價值? 我想在javascript中獲取php值,以便在我的項目中進一步執行。如何在JavaScript中提醒PHP值?

+0

取代echo只是使用警報(測試)腳本標籤中 – krishna

+0

'回聲 '警報( 「測試」)';' –

+0

@krishna這是PHP文件。 – Rebel

回答

1

你爲什麼要分配變量到JavaScript變量,然後呼應呢?聽起來像是額外的工作,我...

echo 'alert("' . $abc . '");'; 
+0

我需要使用那個變量,那就是爲什麼。 – Rebel

+0

這是行不通的。你仍然需要這個變量的雙引號,例如:'echo'alert(''。$ abc。'「);';' –

+0

@ICanHasCheezburger啊是的。謝謝 – serakfalcon

3

試試這個代碼

<?php 
     echo '<script language="javascript">alert("'.$test.'"); </script>'; 
    ?> 

這是你使用JavaScript和PHP更新的代碼

<?php 
$user = "rebel"; 

echo '<script> var name = "'.$user.'"; 
alert(name);</script>'; 

?> 

這是第三代碼,如果你這不工作,那麼你還有其他問題

<?php 
$abc= "Hello"; 
?> 
<script type="text/javascript"> 

    var test="<?php echo $abc; ?>"; 
</script> 

<?php 
    echo '<script language="javascript">'; 
    echo 'alert(test)'; 
    echo '</script>'; 
?> 
+0

這給了我空白值.. – Rebel

+0

@Rebel你會得到警覺嗎?如果是,那麼檢查你的變量 – Prafulla

+0

我已經使用了,var test =「<?php echo $ abc;?>」; – Rebel

0

您的代碼不是那麼清楚,我想你想是這樣的:

<?php 
    $test = "hi there!"; 
    echo '<script type="text/javascript">'; 
     echo 'alert("'.$test.'")'; 
    echo '</script>'; 
?> 
+0

測試是JavaScript變量,而不是PHP變量先生。 – Rebel

0
var test="<?php echo $abc; ?>" 
    <script language="javascript">alert(test); </script> 
+0

我喜歡提醒javascript變量 – Rebel

+0

然後警告(測試);這就是它 – SagarPPanchal

+0

不發生先生.. – Rebel

0

如果你想提醒的$值ABC,你一定要逃逸以下行的斜線與反斜線, 喜歡這個。

<?php $abc = "<script>document.write(jvalue)<\/script>" ?> 

您還需要去掉這一行(或逃避$ ABC的<和>)。如果你不這樣做,腳本標籤會弄亂呈現的html代碼。

<?php echo 'php_'.$abc; ?> 
+1

不,你不需要那樣做。 – Styphon

+0

@Styphon如果你想提醒字符串「」,你確實需要跳過斜槓(因爲腳本標籤)...請看這裏:https:// www。 wwco.com/~wls/blog/2007/04/25/using-script-in-a-javascript-literal/ – Johan