Copain
Copain

Reputation: 183

From laravel controller passing value to ajax then get the particular field

how can i get the ID or Name Field then append it to Textbox, Please Correct me if i'm approaching this in the wrong way.

$(document).ready(function(){
    var students = {!! json_encode($students->toArray()) !!};

    //This Part Im Trying to get.
    var name=$(students).val(name);
    var id=$(students).val(id);

    console.log(name);

});

Controller:

 $students=Student::all()->take(5);
 return view('Examples.levels',compact('students'));

image below is the result if i use console.log(students) enter image description here

Updated Version

$(document).ready(function(){
    var students = {!! json_encode($students->toArray()) !!};
    students.forEach(function(student) {
  $('.content').append(
  `<input name="name[]" value=`+student.name+`>`
  );
  console.log(student.name)
});
});

Attached imaged result: enter image description here

Upvotes: 0

Views: 63

Answers (2)

tohasanali
tohasanali

Reputation: 934

Try this one. it will append with value.

students.forEach(function(student) {
  $('.content').append(
  `<input name="name[]" value="`+student.name+`">`
  );
});

Upvotes: 1

Pavel Lint
Pavel Lint

Reputation: 3527

You should iterate over a students array like so:

students.forEach(function(student) {
   console.log(student.name);
   console.log(student.id);
});

Upvotes: 0

Related Questions