3

I want to ask that how can I call a value from javascript to html blade in laravel.

Here is my JavaScript code:

<script type="text/javascript">
    function valueFunction(){
        var output = document.getElementById('teacherId').value;
        console.log(output);
        document.getElementById('output').innerHTML = output;
    }
</script>

Here is my Laravel Html:

<select id="teacherId" onchange="valueFunction()">
                        <option>Select Teacher</option>
                        @foreach($users as $user)
                        <option value="{{$user->id}}">{{$user->name}}</option>
                        @endforeach
                    </select>
{{$users[]->name}}
<output id="output"></output>

Controller Code:

$users = User::all();
    $students = Students::all();
    return view('admin.assignStudent', compact('users','students'));

As I am getting the value in output tag, but I want to call the value of javascrpt in {{$users[output]->name}} here.

Thank you.

5
  • change <output id="output"></output> . to <div id="output"></div> Commented Feb 1, 2019 at 6:52
  • Your current solution is the best ! Print value with mixing javascript and php variable is a bad idea .. Commented Feb 1, 2019 at 6:52
  • I want to print detail about the selected user thats why i need id to get the data from database Commented Feb 1, 2019 at 7:01
  • can you show your controller code? thanks Commented Feb 1, 2019 at 7:44
  • added controller code Commented Feb 1, 2019 at 9:25

4 Answers 4

1

In laravel or any other PHP framework you have to call java script function as follows:

{{'<script>valueFunction()</script>'}}

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

Comments

0

You can't do it directly, as JavaScript is only executed in the client side without interaction with the blade file.

A suggestion is to set up an API in Laravel, use ajax, XMLHttpRequest or fetch in JavaScript and POST (or other HTTP verbs) to the route. Process and return the data in Laravel. Finally display it in the DOM using JavaScript.

Or if you want the blade template to use the value directly, you may want to use a HTML form to post to Laravel, process the data, then return the view along with the data. Doing so requires the user to navigate to another page (or to the same page again).

Comments

0

Laravel Html

<script type="text/javascript">
    var users = @json($users);
    function valueFunction(){
        var userId = document.getElementById('teacherId').value;
        var user = array.filter(function(arr){return arr.id == userId})[0];
        console.log(user);
        document.getElementById('output').innerHTML = user.name;
    }
</script>

7 Comments

Thanks for your answer, but i want to call it dynamically, as: var outputs = document.getElementById('teacherId').value; document.getElementById('output').innerHTML = '{{$user[outputs]->name}}'; but i cant call output in '{{$user[outputs]->name}}', it is returning error **Use of undefined constant outputs **
My English is not good. Can you describe input and output, pattern?
No problem, the output is the value from dropdown menu in html, dropdown menu is getting options from loop. so m getting selected value in javascript and want to send value to html to print the value in html.
I really want to help you, but I really don't understand what you want. Avoid losing your time, others may help better
will edit my code in question, it may help you to understand it better
|
0

try this and see if it works:

<script type="text/javascript">
    function valueFunction(){
        var output = document.getElementById('teacherId').value;
        console.log(output);


        var data = {!! $sites[output]->name() !!};
        document.getElementById('output').innerHTML = data ;
    }
</script>

1 Comment

Thank you for replying, Tested this getting error : Use of undefined constant output - assumed 'output'

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.