Currently I have used JavaScript to display an alert to notify that the execution was successful, merely for testing purposes. How can I do that in PHP object-oriented style?
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. 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.
Javascript is client-side, which means it runs on the user's machine.
The server should have no control over anything on the user's machine.
Therefore, an alert box from PHP is not possible. :)
You've got to stick with Javascript, which only runs client-side. 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).
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.
Doing this will dump the contents directly on the page source (but not as a browser popup).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.