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).