Home » Php » Double entry of each input type text in the database laravel javacript using foreach

Double entry of each input type text in the database laravel javacript using foreach

Posted by: admin June 30, 2018 Leave a comment

Questions:

i have this in my database.

Person | Location

student1 San Francisco

student2 San Francisco

student1 New York

student2 New york

There’s a problem in my controller. here is the code:

foreach((array)$request->stud as $person){
  foreach((array)$request->studlocation as $location){
    $hm = new HouseMate;

    $hmid = rand();
    $hm->hmid=$hmid;
    $hm->rId_fk=$renterid;
    $hm->person=$person;  

    $hm->location=$location;
    $hm->save();
  }
  $hm->delete();
}

And in my view:

$(document).ready(function() {
  $("#dropdown").change(function() {
    var selVal = $(this).val();
    $("#textboxDiv").html('');
    $("#schoolloc").html('');

    if(selVal > 0) {
      for(var i = 1; i<= selVal; i++) {
        $("#textboxDiv").append('<input type="hidden" value="student'+i+'" name="stud[]" />');
        $("#schoolloc").append('<input type="text" name="studlocation[]" id="studlocation'+i+'" />');
        var input = document.getElementById('studlocation'+i+'');
        var autocomplete = new google.maps.places.Autocomplete(input);

        autocomplete.addListener('place_changed', function () {
          var place = autocomplete.getPlace();
        });
      }
    }
  });
Answers: