this is a noob question.
my javascript function(part of knockout.js model that I have defined):
self.loadData = function(){
alert("loadData got called");
$.ajax({
url: 'database_connection.php',
dataType: 'json',
success: function(data){ //json string of records returned from server
alert('success from server call');
},
error: function(){
alert('error from server call');
}
});
};
Contents of database_connection.php:
<?php
echo "this is called";
$db = new MySqli('localhost', 'username', 'password', 'database');
$activities = $db->query("SELECT * FROM MainActivity");
$activities_r = array();
while($row = $activities->fetch_array()){
$val = $row['mActivityID'];
$act = $row['Name'];
$activities_r[] = array('val'=>$val, 'act' => $act);
}
echo json_encode($activities_r);
?>
The php is correct, coz if I directly access this file through browser, it correctly displays the result from database table.
However, when executed through the loadData
function, I get two alerts:
1. "loadData is called"
2. "error from server call"
the first line of database_connection.php is not being executed since I cant see the result of echo
, so that means the script is not getting called.
Am I using the ajax
function wrongly?