Reputation: 313
For some reason the following ajax success function is not working
What am I trying to do?
The AJAX call:
$('#datatable').on( 'click', '.recipeFlavours', function (e) {
var token = '<?php echo json_encode($token); ?>';
var flavourList = true;
var table = $('#datatable').DataTable();
var rowSelector;
var li = $(this).closest('li');
if ( li.length ) {
rowSelector = table.cell( li ).index().row;
}
else {
rowSelector = $(this).closest('tr');
}
var recipeID = table.row(rowSelector).data().recipe_id;
$.ajax({
type: "POST",
url: "controllers/recipeControl.php",
data: { token: token, recipeID: recipeID, flavourList: flavourList },
success: function(data){
$(".success_container").html(data);
console.log(data);
var popup = '<div class="modal fade" id="notes-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;"><div class="modal-dialog"><div class="notes-modal-container"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button><h1></h1><h3>Recipe Flavours</h3><br>' + data + '</div></div></div>';
$(popup).modal('toggle');
},
});
return false;
});
The Controller: Works fine as PHP
without the json_encode()
/**
GET RECIPE FLAVOURS FOR POPUP
*/
if(isset($_POST['flavourList'])) {
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && isset($_POST['token']) && json_decode($_POST['token']) === $_SESSION['token']){
//echo json_encode($Recipes->getRecipeFlavours($recipeid));
$html = "<div>";
$content = $Recipes->getRecipeFlavours($_POST['recipeID']);
foreach($content as $value) {
$html .= $value['recipe_flavour_name'].$value['recipe_flavour_percent'];
}
$html .= "</div>";
echo json_encode($html);
}
}
The function to get the flavours and percents:
/**
GET RECIPE FLAVOUR LIST
*/
public function getRecipeFlavours($recipeid) {
$query = 'SELECT recipe_flavour_name, recipe_flavour_percent FROM recipe_flavours WHERE recipe_flavour_recipe_id = :recipeid';
$stmt = $this->queryIt($query);
$stmt = $this->bind(':recipeid',$recipeid);
if($this->execute()) {
return $this->resultset();
}
}
As PHP this is the output:
As the success data in AJAX:
Upvotes: 1
Views: 976
Reputation: 9476
try this code:
var obj = jQuery.parseJSON(data);
$(".success_container").html(obj);
console.log(obj);
var popup = '<div class="modal fade" id="notes-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;"><div class="modal-dialog"><div class="notes-modal-container"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button><h1></h1><h3>Recipe Flavours</h3><br>' + obj + '</div></div></div>';
$('#test1').html(popup);
alert(popup)
Upvotes: 0
Reputation: 1022
You are using json_encode while returning the response in controller, but not decoding while process the response in javascript.
So please remove json_encode and try. It will work.
Upvotes: 1
Reputation:
Remove json_encode() because you are using HTML as Datatype for ajax
if(isset($_POST['flavourList'])) {
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && isset($_POST['token']) && json_decode($_POST['token']) === $_SESSION['token']){
//echo json_encode($Recipes->getRecipeFlavours($recipeid));
$html = "<div>";
$content = $Recipes->getRecipeFlavours($_POST['recipeID']);
foreach($content as $value) {
$html .= $value['recipe_flavour_name'].$value['recipe_flavour_percent'];
}
$html .= "</div>";
echo $html;
}
}
Upvotes: 1