I would like to call some function from a plugin I am writing. I call the plugin code through a shortcode on my wordpress page. To start, I am simply trying to pop an 'alert' window, say, if I am successful in connecting to the Database. Eventually, the plugin will display drop downs one below the other, the later ones being populated based on the selection in previous one. Appreciate help! Code in my plugin is as follows:
<?php
/*
Plugin Name: Custom program graphs
Plugin URI: localhost/test/
Description: Creates graphs
Version: 0.1.0
Author: My Cars
*/
require_once("dbcontroller.php"); # Instantiate DB Controlling - WORKS WELL functions
function vc_connect_to_database()
{
$db_handle = new DBController();
$connection = $db_handle->connectDB();
if (!$connection)
{
return '<h6>' . "Unable to connect to database." . '</h6>';
}
# WORKS WELL IN THE CONDITION ABOVE THIS
else
{
# FAILS HERE!! HOW CAN I CALL A FUNCTION LIKE alert FROM HERE?
return window.alert("Connected to the database successfully.");
}
}
add_shortcode('connect_database', 'vc_connect_to_database');
window.alert
isn't a PHP function, it's a javascript function, you can't call Javascript functions in PHP and vice versa. I also wouldn't recommend shortcodes for things like connecting to the database. There is such a thing as being too granular