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警报吗?如果有,怎么样?
7 个解决方案
#1
12
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代码时,没有与浏览器的联系(直到响应开始返回)。在某些情况下,服务器可能会将内容流式传输回浏览器,在这种情况下,可能会有一些商定的约定,服务器可以使用消息立即标记内容流,但是我不认为这就是你在这里问的问题。
#2
22
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.
您还可以从服务器回显Javascript(不使用HTML),然后通过动态创建包含该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 Firebug for Firefox.
如果要跟踪Javascript,请参阅Firebug for Firefox。
PHP Headers API Documentation
On-demand Javascript at Ajax Patterns
PHP标头API文档Ajax模式的按需Javascript
#3
6
Yes, you can.
是的你可以。
echo "<script>alert (\"js inside php\")</script>";
#4
3
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>
?>
#5
0
Yes, if you send Javascript code with alert
to a html page from PHP.
是的,如果您将带有警报的Javascript代码发送到PHP的html页面。
No, if you want to call alert
just by executing server side code and not sending JS to client browser.
不,如果您只想通过执行服务器端代码而不是将JS发送到客户端浏览器来调用警报。
#6
0
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部分。
#7
0
You can output a Javascript alert() within a PHP function in at least two ways:
您可以通过至少两种方式在PHP函数中输出Javascript警报():
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>";
}
#1
12
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代码时,没有与浏览器的联系(直到响应开始返回)。在某些情况下,服务器可能会将内容流式传输回浏览器,在这种情况下,可能会有一些商定的约定,服务器可以使用消息立即标记内容流,但是我不认为这就是你在这里问的问题。
#2
22
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.
您还可以从服务器回显Javascript(不使用HTML),然后通过动态创建包含该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 Firebug for Firefox.
如果要跟踪Javascript,请参阅Firebug for Firefox。
PHP Headers API Documentation
On-demand Javascript at Ajax Patterns
PHP标头API文档Ajax模式的按需Javascript
#3
6
Yes, you can.
是的你可以。
echo "<script>alert (\"js inside php\")</script>";
#4
3
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>
?>
#5
0
Yes, if you send Javascript code with alert
to a html page from PHP.
是的,如果您将带有警报的Javascript代码发送到PHP的html页面。
No, if you want to call alert
just by executing server side code and not sending JS to client browser.
不,如果您只想通过执行服务器端代码而不是将JS发送到客户端浏览器来调用警报。
#6
0
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部分。
#7
0
You can output a Javascript alert() within a PHP function in at least two ways:
您可以通过至少两种方式在PHP函数中输出Javascript警报():
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>";
}