0

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") }}

3
  • 2
    You can't combine PHP and JavaScript like this. PHP is a server-side language. JavaScript is a client-side language. When PHP (the server) runs the code, there is no d_from value because that's run on the client's end. Commented Nov 16, 2015 at 19:08
  • 2
    You're mixing javascript and PHP here, the d_from variable is a javascript variable that won't be available to PHP since it is only on the client side. Commented Nov 16, 2015 at 19:08
  • 1
    You could do something like that with an AJAX request, if you prepare a method that returns a JSON for example (and Laravel helps you a lot here). Commented Nov 16, 2015 at 19:10

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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>');
});
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.