如何在 javascript 警报框中添加 php 内容或变量?!javascript, php, 警报框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18672074/
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-25 18:00:00  来源:igfitidea点击:

How to add php content or variable inside javascript alert box?! javascript, php, alertbox

phpjavascript

提问by Pooja

How can i add php content or php variable inside Java-script alert box?! I tried to make it work few ways but it is only popping up a blank box rather than the contents of php variable.

如何在 Java 脚本警报框中添加 php 内容或 php 变量?!我试图让它以几种方式工作,但它只是弹出一个空白框,而不是 php 变量的内容。

Here is the code:

这是代码:

<script language="javascript">
    $(document).ready(function() {
        $("#a").blur(function() {           
            <?php $b = $_POST['a'];

            if(isset($_POST['update'])) {
            mysql_query("UPDATE tbl_travel set fld_a='".$_POST[$b]."' where fld_id = '".$_POST["id"]."' ") or die(mysql_error());
            } ?>

                alert (<?php $b ?>);
           });
    });
</script> 

Thank You for your Help :)

感谢您的帮助 :)

回答by Sergio

Change this

改变这个

alert (<?php $b ?>);

to this

对此

alert ('<?php echo $b; ?>');

You need to output the value of $band add quotes inside the alert.

您需要$b在警报中输出值并添加引号。

About PHP - echo

关于 PHP -回声

回答by mkas

Have you tried this?

你试过这个吗?

alert ('<?php echo $b ?>');

回答by Zafer BAHADIR

I use it like this

我像这样使用它

$text="Example PHP Variable Message";
echo '<script type="text/javascript">alert("'.$text.'")</script>';

回答by jignesh

1.

1.

<script>
  alert("</script><?php $r=5;echo $r;?> <script>")
</script>

you have to script off on when php start

你必须在 php 启动时关闭脚本

回答by Praveesh P

This worked for me :

这对我有用:

alert ("<?php echo $b; ?>");