我可以在 PHP 函数中添加 javascript 警报吗?如果是,如何?

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

Can I add a javascript alert inside a PHP function? If yes, how?

phpjavascript

提问by Ahmad Farid

Possible Duplicate:
How to call a JavaScript function from PHP?

可能的重复:
如何从 PHP 调用 JavaScript 函数?

Can I add a javascript alert inside a PHP function? If yes, how?

我可以在 PHP 函数中添加 javascript 警报吗?如果是,如何?

回答by Aiden Bell

Yes, you can, though I 100% guarantee this isn't what you want or what you mean:

是的,你可以,但我100% 保证这不是你想要的或你的意思

<?php
    function do_alert($msg) 
    {
        echo '<script type="text/javascript">alert("' . $msg . '"); </script>';
    }
?>
<html><head><title>Hello</title></head>
<body>
<h1>Hello World, THis is my page</h1>
<?php
    do_alert("Hello");
?>
</body>
</html>

The browser runs the Javascript, the server runs the PHP.

浏览器运行 Javascript,服务器运行 PHP。

You could also echo Javascript from your server (without HTML) and then include that script into your page by dynamically creating a <script>tag containing that Javascript. This is essentially creating Javascript on the fly for injection into your page with the right headers etc.

您还可以从您的服务器(不带 HTML)回显 Javascript,然后通过动态创建<script>包含该 Javascript的标记将该脚本包含到您的页面中。这本质上是动态创建 Javascript 以使用正确的标题等注入您的页面。

If you want to trace some PHP script execution, then you can use trigger_error()to create a log entry, or you could write a trace()function to store strings in a buffer and add them to a page.

如果你想跟踪一些 PHP 脚本的执行,那么你可以使用trigger_error()来创建一个日志条目,或者你可以编写一个trace()函数来将字符串存储在缓冲区中并将它们添加到页面中。

If you want to trace Javascript, See Firebugfor Firefox.

如果您想跟踪 Javascript,请参阅Firebugfor Firefox。

PHP Headers API Documentation
On-demand Javascript at Ajax Patterns

PHP 标头 API 文档
Ajax 模式中的按需 Javascript

回答by Pointy

Assuming that what you mean is that you want an alert to pop up from the browser when the server reaches a certain point in your php code, the answer is "no".

假设您的意思是当服务器到达您的 php 代码中的某个点时,您希望从浏览器中弹出警报,那么答案是否定的。

While the server is running your php code, there's no contact with the browser (until the response starts coming back). There are situations in which a server may be streaming content back to the browser, and in such a case it'd be possible to have some agreed-upon convention for the server to mark the content stream with messages to be alerted out immediately, but I don't think that's what you're asking about here.

当服务器运行您的 php 代码时,没有与浏览器的联系(直到响应开始返回)。在某些情况下,服务器可能会将内容流式传输回浏览器,在这种情况下,可能有一些商定的约定,服务器可以使用要立即发出警报的消息来标记内容流,但是我不认为这就是你在这里问的。

回答by atlavis

Yes, you can.

是的你可以。

echo "<script>alert (\"js inside php\")</script>";

回答by Rhapsody

To explain; PHP is compiled at the server and the result is plain HTML. Therefore alerts and such cannot appear while compiling is a silent process.

解释; PHP 在服务器上编译,结果是纯 HTML。因此,在编译是一个静默过程时不会出现警报等。

If you want to show an alert in the HTML generated by PHP;

如果要在 PHP 生成的 HTML 中显示警报;

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

回答by mck89

echo "<script type='text/javascript'>alert('alert text goes here');</script>";

But i don't know how it can be useful because it will work only on the client side. If you want to debug your code you can simply print it on the screen, but if you use the alert take care of quotes because they can break the javascript part.

但我不知道它如何有用,因为它只能在客户端工作。如果你想调试你的代码,你可以简单地将它打印在屏幕上,但如果你使用警报,请注意引号,因为它们可能会破坏 javascript 部分。

回答by tommytwoeyes

You can output a Javascript alert() within a PHP function in at least two ways:

您可以通过至少两种方式在 PHP 函数中输出 Javascript alert():

A) Return it:

A) 退货:

function sendAlert($text) {
    return "<script type='text/javascript'>alert('{$text}');</script>";
}

B: Output it directly:

B:直接输出:

function echoAlert($text) {
    echo "<script type='text/javascript'>alert('{$text}');</script>";
}

回答by darioo

Yes, if you send Javascript code with alertto a html page from PHP.

是的,如果您将 Javascript 代码alert从 PHP发送到 html 页面。

No, if you want to call alertjust by executing server side code and not sending JS to client browser.

不,如果您alert只想通过执行服务器端代码而不是将 JS 发送到客户端浏览器来调用。