从JavaScript回声PHP变量? [英] Echo PHP variable from JavaScript?

查看:93
本文介绍了从JavaScript回声PHP变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PHP页面上也有一些JavaScript代码,但是下面的JavaScript代码似乎不起作用,或者我可能没有办法了!



我我正在尝试如下所示:

  var areaOption = document.getElementById(<?php echo @ $ _ POST ['annonsera_name' ]>?); 
areaOption.selected = true;

我也尝试过这种方法,但它仅提醒BLANK警报框:

  alert(<?php echo $ test;?>); //我试过用引号,双引号等等......没有运气

我是想在这里完全错误吗?
$ b

更新

一些PHP代码:

 <?php 
$ test =Hello World!;
?>


解决方案

在第二个例子中,字符串(所以H被解释为一个变量 - 你没有设置)。



测试这个:

  alert(<?php echo'H';?>); 

OR

  alert('<?php echoH;?>'); 


I have a PHP page with some JavaScript code also, but this JavaScript code below doesn't seem to work, or maybe I'm way off!

I am trying something like this:

  var areaOption=document.getElementById("<?php echo @$_POST['annonsera_name']?>");
  areaOption.selected=true;

Also I have tried this, but it only alerts a BLANK alert-box:

    alert (<?php echo $test;?>); // I have tried this with quotes, double-quotes, etc... no luck

Am I thinking completely wrong here?

UPDATE

Some PHP code:

    <?php 
        $test = "Hello World!";
    ?>

解决方案

In your second example, you are missing quotes around the string (so H is interpreted as a variable - which you didn't set).

Test this:

alert (<?php echo "'H'";?>);

OR

alert ('<?php echo "H";?>');

这篇关于从JavaScript回声PHP变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆