What I'm using: JQuery, mysql_crud.php class
I have a class for donor, inside the class I created a function to add a donor into the database. Any result made by the function will return an array.
donor-class.php
public function addDonor($foo, $bar, $soap, ){
.
.
$message = array('message' => 'Message for user here');
return $message;
}
The class is then executed by another php file which will return JSON data
add-donor-function.php
$res = $donor->addDonor($foo, $bar, $soap);
header('Content-Type: application/json');
echo json_encode($res);
This PHP file will return JSON data
{
"message" : "Message for user here"
}
I also have a javascript file to run an ajax function
js_add_donor.js
function addDonor() {
var name = $('#name').val();
var ic_no = $('#ic_no').val();
var gender = $('#gender').find(":selected").val();
var email = $('#email').val();
var mobile = $('#mobile').val();
var b_type = $('#b_type').find(":selected").val();
var dob = $('#don').val();
var address = $('#address').val();
var district = $('#district').find(":selected").val();
$.ajax({
url: "/firstblood/php/function/donor-management/add-donor-function.php",
type: "POST",
data: {name:name, ic_no:ic_no, gender:gender, email:email, mobile:mobile, b_type:b_type, dob:dob, address:address, district:district},
dataType: 'json',
success: function(result, status){
// Below codes won't run
alert('heloo');
alert(status);
$('#response').text(success.message);
}, error: function(xhr, status, error) {
// Below codes run
alert(xhr.status);
alert(status);
alert(error);
}
});
return false;
}
addDonor() is used for a button when clicked.
When the code is executed, the window alerts "200", "parseerror", and "SyntaxError: Unexpected token < in JSON at position 0"
Where am i wrong here ?