1

Want to get details from database in laravel project according to user selection, for this have created a drop-down list from where the user selects any id which is further sent to javascript. Here is laravel code:

<div class="form-group">
    <label class="col-lg-2 col-sm-2 control-label">Select Teacher</label>
    <div class="col-lg-10">
        <select class="form-control  select2" name="teacher" id="teacher" onchange="teacherFunction()" required autofocus>
            <option value="null">Select Teacher</option>
                @foreach($users as $user)
                    <option value="{{$user->id}}">{{$user->name}}</option>
                @endforeach
        </select> 
        <p class="help-block"></p>
    </div>
</div> 

this is working fine and data is being sent to java-script function:

Here is javascript code:

function teacherFunction(){
   var output = document.getElementById('teacher').value;
   console.log(output);
   var test = Number(output);
   console.log();
   console.log(test);
   document.getElementById('output').innerHTML = '@foreach($users as $user) @if($user->id == '+1+') Test @endif @endforeach';
}

in this when output is being sent via innerHTML is i use id hard coded its working fine when i use the output value which user have selected then the if condition is not working. Have tried:

JSON.stringify()
JSON.parse()
parseINT()
Number()

All these didn't worked. Any Solution for this will be appreciated.

4
  • the output works because it is run on the server side, you need to save all the users in an array in javascript to be able to access it in the session Commented Feb 26, 2019 at 10:28
  • Let me know if it gives any error stackoverflow.com/questions/54883313/… Commented Feb 26, 2019 at 10:40
  • @Googlian it's working as expected thanks for help. Commented Feb 26, 2019 at 10:45
  • You are welcome, Happy Coding :) Commented Feb 26, 2019 at 10:51

2 Answers 2

1

Your JavaScript code should look like below, you cannot type your validation in JavaScript as you did, because server-side code is executed first.

function teacherFunction(){
   var output = document.getElementById('teacher').value;
   console.log(output);
   var test = Number(output);
   console.log();
   console.log(test);

   var data = {!! json_encode($users) !!};

   for(int x = 0; x < data.length; x++) {
     if(data[x].id == output) {
       document.getElementById('output').innerHTML = 'TEST';
     }
   }

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

Comments

0

PHP is run server side, this means that JavaScript can't interact with PHP directly. By the time your function is executed it doesn't look like

document.getElementById('output').innerHTML = '@foreach($users as $user) @if($user->id == '+1+') Test @endif @endforeach';

but like

document.getElementById('output').innerHTML = 'test';

To be able to access the entire list of users and print there information accordingly you could put the collection into a JavaScript variable

let users = {{json_encode($users)}};

another way is to use ajax or $.get(); to retrieve it from the server once selected

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.