[英]How can I display an alert/ message in object-oriented PHP?
Currently I have used JavaScript to display an alert to notify that the execution was successful, merely for testing purposes. 目前,我已经使用JavaScript来显示警报,以通知执行成功,仅出于测试目的。 How can I do that in PHP object-oriented style?
我该如何以PHP的面向对象样式进行操作?
I've tried: 我试过了:
public $msg='May the force be with you.';
$this->msg = new msg();
...but result was a white blank page. ...但是结果是白色的空白页。 JavaScript piece I tried works well though.
我尝试过的JavaScript片段效果很好。 Below is the complete code:
以下是完整的代码:
<?php
class database {
public $mysql;
private $db_host='localhost';
private $db_username='admin';
private $db_password='password';
private $db_name='db';
function __construct() {
$this->mysql = new mysqli($this->db_host, $this->db_username, $this->db_password, $this->db_name) or die (mysql_error() ); {
echo
"<script type='text/javascript'>
alert ('The Force will be with you, always.');
</script>";
}
}
function __destruct() {
$this->mysql->close();
}
}
?>
PHP is server-side, which means it runs on a machine different from the user. PHP是服务器端的,这意味着它在与用户不同的计算机上运行。
Javascript is client-side, which means it runs on the user's machine. Javascript是客户端的,这意味着它可以在用户的计算机上运行。
The server should have no control over anything on the user's machine. 服务器不应控制用户计算机上的任何内容。
Therefore, an alert box from PHP is not possible. 因此,无法使用PHP发出警告框。 :)
:)
You've got to stick with Javascript, which only runs client-side. 您必须坚持只在客户端运行的Javascript。 That, or echo plaintext out onto the document itself.
那,或者将纯文本回显到文档本身上。
FYI, this relationship brings up other fun questions, like, "how can Javascript talk to the server" (answer:ajax) and "can a script talk to another computer" (answer: yes, but it's not supposed to). 仅供参考,这种关系带来了其他有趣的问题,例如,“ Javascript如何与服务器对话”(answer:ajax)和“脚本能否与另一台计算机对话”(答案:是的,但是不应该这样)。
Try this code: 试试这个代码:
<?php
class database {
public $mysql;
private $db_host='localhost';
private $db_username='admin';
private $db_password='correcthorsebatterystaple';
private $db_name='projekt';
function __construct() {
$this->mysql = new mysqli($this->db_host, $this->db_username, $this->db_password, $this->db_name) or die (mysql_error() );
}
function pri()
{
echo
"<script type='text/javascript'>
alert ('The Force will be with you, always.');
</script>";
}
function __destruct() {
$this->mysql->close();
}
}
$msg=new database();
$msg->pri();
?>
In PHP, you can do print_r()
for any variable (or var_dump()
) to see the contents of an object or array, etc. 在PHP中,您可以对任何变量(或
var_dump()
)执行print_r()
来查看对象或数组等的内容。
Doing this will dump the contents directly on the page source (but not as a browser popup). 这样做会将内容直接转储到页面源上(但不会作为浏览器弹出窗口)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.