i am having a little trouble with the following ajax call;
function question() {
//ajax call to fetch data from database
var course = "St.Andrews";
var dataString = "course=" + course;
$.ajax({
type: "GET",
url: "http://www.webaddress/fetch.php",
datatype: "json",
data: course,
success: function(datas){
console.log(datas);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
console.log("error:" + XMLHttpRequest.responsetext);
}
});
}
For some reason, i cannot get the fetched results to display. My php file that returns the results works fine if i navigate to it from the browser and i get the returned results in the valid format.
When i check the console log, i can see the params are correct and i get error:undefined. Can anyone provide as to what i'm doing wrong, thanks.
Here is my php script;
<?php
//include databse details
require_once 'login.php';
//connect to database or return error
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("unable to connect to MYSQL:" . mysql_error());
//select database or return error
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
if (isset($_GET['course'])) {
$course = $_GET['course'];
//set the character code
mysql_query('SET CHARACTER SET utf8');
//make the query
$query = "SELECT * FROM questions WHERE course = '" . $course . "' ";
$result = mysql_query($query) or die (mysql_error());
if ($result !== false && mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$question = $row['question'];
$answer = $row['answer'];
$incorrect = $row['incorrectAnswer'];
$difficulty = $row['difficulty'];
//Json row
$json = array ("question" => $question, "answer" => $answer, "incorrect" => $incorrect, "difficulty" => $difficulty);
} else {
//catch any errors
$json = array("error" => "Mysql query error");
}
//set header for json data
header("Content-Type: application/json", true);
//return Json
echo json_encode($json);
} else {
echo "Needs course to advance dingbat";
}
console.log()more in the error CB.