[英]How can I display an alert/ message in object-oriented PHP?
目前,我已經使用JavaScript來顯示警報,以通知執行成功,僅出於測試目的。 我該如何以PHP的面向對象樣式進行操作?
我試過了:
public $msg='May the force be with you.';
$this->msg = new msg();
...但是結果是白色的空白頁。 我嘗試過的JavaScript片段效果很好。 以下是完整的代碼:
<?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是服務器端的,這意味着它在與用戶不同的計算機上運行。
Javascript是客戶端的,這意味着它可以在用戶的計算機上運行。
服務器不應控制用戶計算機上的任何內容。
因此,無法使用PHP發出警告框。 :)
您必須堅持只在客戶端運行的Javascript。 那,或者將純文本回顯到文檔本身上。
僅供參考,這種關系帶來了其他有趣的問題,例如,“ Javascript如何與服務器對話”(answer:ajax)和“腳本能否與另一台計算機對話”(答案:是的,但是不應該這樣)。
試試這個代碼:
<?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();
?>
在PHP中,您可以對任何變量(或var_dump()
)執行print_r()
來查看對象或數組等的內容。
這樣做會將內容直接轉儲到頁面源上(但不會作為瀏覽器彈出窗口)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.