I'm trying to create a dynamic view with laravel, php and jquery ajax. When a navbar button is clicked, it will send an ajax request to the controller which will verify the id send, get the data and return this in html format.
Since there might be multiple values send back which need their own html div's. So I've tried this using an object.
buttonclicked => ajax request to controller => Get data => format data with html => put it in an object => return object to the ajax request succes: .
The problem I'm having now is that The object does return, but instead of the html formatted data, it has a boolean set to true as shown in firebug.
I'm fairly new to Ajax and Jquery, and I'm sorry if this question has been asked many times before.
Ajax:
function onclickNavbar(id) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url:'/controller/AjaxController',
type: "POST",
dataType: "json",
beforeSend: function (xhr) {
var token = $('meta[name="csrf_token"]').attr('content');
if (token) {
return xhr.setRequestHeader('X-CSRF-TOKEN', token);
}
},
data: {identifier: id},
success: function(data){
var $Htmlselector = $("#contenthtml");
console.log('succes');
jQuery.each(data.html, function(key, value){
console.log(key + ' : ' + value);
$($Htmlselector).html(value);
});
},
error: function(data){
console.log('Error something went wrong. See response output:');
console.log(data);
}
});
}
Controller:
public function processor(){
if(isset($_POST['identifier'])){
$id = $_POST['identifier'];
$db = new DatabaseController();
$diensten = $db->GetAllDiensten()->where('parentcategory_id', '=', $id);
$htmlObject = array();
$counter = 0;
foreach($diensten as $dienst){
$htmlObject[$counter++] = "
<div class='col-xs-2half card-column'>
<div class='article-list header-card'>
<a href='/dienst/" . $dienst->shortname . "'>
<img src=" . asset( 'assets/img' . $dienst->image ) . "/>
</a>
</div>
<div class=".'article-list'." class=".'body-card'.">
<hr class=".'hr-card'." >
@if(" . strlen($dienst->name) <= 20 . ")
<h3 class='name text-card'>" . $dienst->name . "</h3>
@else
<h3 class='name text-card'>" . $dienst->name . "</h3>
@endif
<hr>
<p class='description idtagA text-card'>" . $dienst->shortdesc . "</p>
</div>
<div class='article-list footer-card'>
<a href='/dienst/". $dienst->shortname ."'>
<button class='btn btn-danger btn-card' type='button' style='padding-bottom:10px;'>
<span>". $dienst->shortname ."</span>
</button>
</a>
</div>
</div>";
}
$htmlObject = json_encode((object)$htmlObject);
return response()->json(['html'=> $htmlObject]);
//return json_encode($html, true);
}else{
return 'Value Identifier not set!';
}
}
note: I am using Laravel 5.6.3, The @if @foreach etc is syntax for blade.
UPDATE: ## Error according to the code of @SmitRaval
{"html":[true]}Array{
"message": "Array to string conversion",
"exception": "ErrorException",
"file": "C:\\xampp\\htdocs\\Development Entric\\Entric_website\\app\\Http\\Controllers\\AJAXController.php",
"line": 49,
"trace": [
{
"file": "C:\\xampp\\htdocs\\Development Entric\\Entric_website\\app\\Http\\Controllers\\AJAXController.php",
"line": 49,
"function": "handleError",
"class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
"type": "->"
},
{
"function": "processor",
"class": "App\\Http\\Controllers\\AJAXController",
"type": "->"
},
{
.... It goes on for a while...
UPDATE 2:
The are more functions like this, they're there now, so when some queries are used more often with the ->where or ->find I can expand these functions with different parameters. Dienstencontroller:
use App\diensten; // This is the model.
class DatabaseController extends Controller
{
function GetAllDiensten(){
return $diensten = diensten::all();
}