I'm trying to learn how to to use javascript(with or without jquery) in an HTML page on the same server as a php class, to initialize the PHP class, then call its getLinks() method and pass its return array of links to a java script array.
I was hoping that this could be done without having any php code on the index page at all.
I have seen a few examples, including this great one Here, but it doesn't seem to address certain particulars such as;
1)How would I even write an include on a page with no php? 2)How would I instantiate a php class , and call a specific method? 2)I can make a PHP helper file to do the instantiations, but then I would still need to call a method, how would this be done? 3)How would I use jQuery to make a GET request and call the function from the request?
If this can not be done without adding php code to the index page, please let me know this
My PHP Code:
<?php
//class.links.php
include "db_config.php";
class Links{
public $db;
public function __construct(){
$this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if(mysqli_connect_errno()){
echo "Error: Could not connect to the database.";
exit;
}
}
public function getLinks(){
$sql = "SELECT link_name FROM links";
$rows = mysqli_query($this->db, $sql) or die(mysqli_connect_errno()."Data can not be inserted.");
$links = array();
if($rows->num_rows < 1){
return false;
}else{
while ($row=mysqli_fetch_row($rows)){
$links[] = $row[0];
}
return $links;
}
}
}
?>
HTML Code:
<html>
<head>
<title>Home Page</title>
<link type="text/css" rel="stylesheet" href="engineering.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script src="scriptfile.js">
</script>
</head>
<body>
<div name ="test_spot" id ="test_spot" width="200" height="400"></div>
<script type = "text/javascript">
function getOutput(){
}
</script>
<button onClick="getOutput()" style="height: 60px; width:100px;" >Test</button>
</body>
</html>
Please show me the script that belongs in the getOutput() method to place the array in the test_spot div.
Thanks