Mario Ene
Mario Ene

Reputation: 853

jQuery syntax in Laravel View

My code in my View (Laravel framework):

...   
var d_from = $('#date_from').val();
$('#total-invoices').html('<a href="#" class="btn btn-xs btn-danger">{{ App\Output_Detail::getTotalInvoices(' + d_from + ') }}</a>');
...

$('#total-invoices') is a HTML Label component.

I have a syntax error in my above code above: ..(' + d_from + ')..

If I put a static variable, the return from my Model function is working fine:

{{ App\Output_Detail::getTotalInvoices("2015-11-03") }}

Upvotes: 0

Views: 93

Answers (3)

Mario Ene
Mario Ene

Reputation: 853

I solved my issue:

...
// ajax
$.get('{{ URL::to('employee/ajax_Total_Invoices') }}?date_from=' + $('#date_from').val(), function(data){
  // success data
  $('#total-invoices').html('<a href="#" class="btn btn-xs btn-danger">'+data+'</a>');
});
...

Upvotes: 0

lebobbi
lebobbi

Reputation: 2277

You cannot have the backend code depend on the front end code in that way.

When your page loads, all the PHP Laravel code gets executed on the Server machine.

Then, all Javascript code gets executed on the resulting page on the clients machine. That will be your computer or the users computers.

So, getTotalInvoces is receiving the string literally as ' + d_from + '.

If you need to get the values after a page has been loaded, you will need to make an ajax call to the server.

Upvotes: 1

Josh KG
Josh KG

Reputation: 5140

You're mixing JavaScript and PHP here. PHP is a server side language, which means it is processed on your server. JavaScript is a client-side language, which is run on the client's browser. So your PHP will be parsed BEFORE your JavaScript. You're trying to use a JavaScript variable d_from inside PHP, but that variable won't be declared until PHP is done and the HTML is sent to the client's browser.

As far as a solution goes -- whatever value you're populating the #date_from input with you could also drop into this getTotalInvoices method. If that value isn't available until it hits the client-side, you'll need to make an AJAX call back to the server to run this method.

Upvotes: 2

Related Questions