在 Javascript 中回显 PHP?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4544648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:53:07  来源:igfitidea点击:

Echo PHP inside Javascript?

phpjavascriptecho

提问by sarthak

Why is this not working?

为什么这不起作用?

<?php 

mysqli_select_db($connect,"dev");
$response = "Select response from revbut where session='$u'";
$rquery  =  mysqli_query($connect,$response);

$responseanswer = mysqli_fetch_array($rquery);
$re  = $responseanswer['response'];
?>

<script type="text/javascript">
<?php echo $re; ?>
</script>

$reinside JavaScript is not getting echoed. But if I place it inside the above PHP function, it is getting echoed.

$reJavaScript 内部没有得到回应。但是如果我把它放在上面的 PHP 函数中,它就会得到回应。

EDIT - BUT THEN WHY IS THIS NOT WORKING?

编辑 - 但是为什么这不起作用?

if(<?php echo $re; ?>){
    document.getElementById('hide').style.display = "none";
}

if I PLACE the hide function outside the if - it is working.

如果我将隐藏功能放在 if 之外 - 它正在工作。

回答by Stefan Gehrig

It get's echoed, but you won't see anything on your page as the text will be written within the Javascript-tag which is not displayed by the browser. Look at your page source to verify that the text is really there.

它得到了回应,但您不会在页面上看到任何内容,因为文本将写入浏览器不显示的 Javascript 标记中。查看您的页面源以验证文本是否真的存在。

EDIT

编辑

Try

尝试

if(<?php echo json_encode($re); ?>){
    document.getElementById('hide').style.display = "none"; 
}

This will ensure that your PHP string will be converted into the appropriate Javascript type - in case of strings it'll ensure that the string is enclosed in "and is escaped properly.

这将确保您的 PHP 字符串将被转换为适当的 Javascript 类型 - 在字符串的情况下,它将确保该字符串包含在其中"并正确转义。

EDIT again

再次编辑

When you do the following

当您执行以下操作时

<script type="text/javascript"> 
if(<?php echo $re; ?>){
    document.getElementById('hide').style.display = "none"; } 
</script>

this is what is written to the HTML page (that's then interpreted by the browser)

这是写入 HTML 页面的内容(然后由浏览器解释)

<script type="text/javascript"> 
if(whatever is in the $re vairable){
    document.getElementById('hide').style.display = "none"; } 
</script>

But this is not even valid Javascript. What you want is

但这甚至不是有效的 Javascript。你想要的是

<script type="text/javascript"> 
if("whatever is in the $re vairable"){
    document.getElementById('hide').style.display = "none"; } 
</script>

Note the "which ensures that the whole thing is valid Javascript and that the contents of $rewill be interpreted as an Javascript string by the Browser's Javascript engine. The call to json_encode()does exactly this - it transforms PHP variables into the appropriate Javascript variables.

请注意"确保整个内容是有效的 Javascript,并且$re浏览器的 Javascript 引擎将其内容解释为 Javascript 字符串。对 的调用json_encode()正是这样做的 - 它将 PHP 变量转换为适当的 Javascript 变量。

回答by Umang Goyal

try this:

尝试这个:

<script type="text/javascript">
alert('<?php echo $re; ?>');
</script>

回答by álvaro González

First, you cannot assume that all DB related operations will always complete sucesfully and will always return data. Open the PHP manual, review all your mysqli_* function calls and add a proper test to detect whether they return an error code or not.

首先,您不能假设所有与数据库相关的操作都将始终成功完成并始终返回数据。打开 PHP 手册,查看所有 mysqli_* 函数调用并添加适当的测试以检测它们是否返回错误代码。

Second, PHP and JavaScript don't run at the same time. After PHP is done all you get is some plain text that's sent to the browser. If that text happens to be JavaScript code, it will be run. And you don't have to guess: the JavaScript code is right there and you can use your browser's View Source menu to inspect it.

其次,PHP 和 JavaScript 不会同时运行。PHP 完成后,您得到的只是一些发送到浏览器的纯文本。如果该文本恰好是 JavaScript 代码,它将被运行。而且您不必猜测:JavaScript 代码就在那里,您可以使用浏览器的“查看源代码”菜单来检查它。

回答by Shakti Singh

do view source it is echoing try it and confirm

做查看源它正在回显尝试并确认

<script type="text/javascript">
alert('<?=$re; ?>');
</script>