Search code examples
javascriptphplaravellaravel-5computer-science

How to add javascripte in larava


How to add javascript in laravel.

Error=>Use of undefined constant s - assumed 's'

<script>
       function run() {

           var a = document.getElementById("save");

           var value = document.getElementById("selectP").value;

           var s = "Referrdetails/"+value;

           a.href = "{{route(s)}}";

       }

</script>

Solution

  • You can't use PHP blade functions with dynamically queried javascript variables from the DOM because Blade views are compiled on the server and the resulting HTML is sent back to the browser as it is before your Javascript even loads, just concatenate a URL string instead

    Find out what the full URL for referrdetails is and concatenate it like so

    a.href = "/referrdetails/" + value;
    

    Or with ES6 templates

    a.href = `/referrdetails/${value}`;
    

    Hope this helps