Is it possible to return a data array in Laravel with ajax?
I basically need to pull in some data without refreshing the page. It is for a payment system I have created: on the form the administrator will select a persons name from a select input field and then input payments against their name which is then stored in the database. The administrators have a problem where they can't see any payment history and sometimes missing payments or adding the same payment more than once.
So the idea is when they select the name an ajax request will fire and pull in payment history based on the persons ID.
I have created the function within my controller which works, and also created AJAX request which works, I just don't know how to return the data and then insert it into a table or some presentable way.
// Ajax request var base_url = 'http://localhost';
$('select.child_id').change(function() {
alert('Select field value has changed to ' + $('select.child_id').val()); //testing + works
var child_id = $('#child_id');
var dataString = 'child_id=' + child_id;
$.ajax({
type: "POST",
url: base_url + "/finance/payment-history",
data: dataString,
success: function(html) {
console.log(html);
return $data // ??????
},
dataType: 'html'
});
return false;
});
public function paymentHistory()
{
$data = Input::all();
if(Request::ajax())
{
$child_id = Input::get('child_id');
$payments = Finance::where('child_id', '=', $child_id)->orderBy('pdate', 'ASC')->get();
}
}
thanks
You could return a HTML view JSON encoded and insert back into the DOM such as
$html = View('admin.comments._comments', $view_data );
return \Response::json(['status' => 'true','html' => $html->render() ]);
(an example from an admin panel in one of my projects)
else just replace the view with an array of data and insert back into the DOM accordingly.
Hope it helps.
jacksoncharles said:
You could return a HTML view JSON encoded and insert back into the DOM such as
$html = View('admin.comments._comments', $view_data ); return \Response::json(['status' => 'true','html' => $html->render() ]); (an example from an admin panel in one of my projects)
else just replace the view with an array of data and insert back into the DOM accordingly.
Hope it helps.
thanks, this works, but i'm not sure of the jQuery part how to I access it in my view?
According to your code it will exist in "html"
success: function(html) {
console.log(html);
return $data // ??????
},
So from there you need to insert the HTML into your webpage replacing or appending to whatever DOM element you want.
success: function(html) {
if( html['status'] == 'true' )
{
$('#some_element').replace(html['html']);
}
else
{
// Status not equal true, do something else
}
},
PS> Replace
success: function(html) {
with something like
success: function(responseData) {
More conventional and you are not always returning HTML
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community