button sends value of field input hidden

1

I have a problem with the use of a specific button in the project. I'll explain: I have a window opened by the window.open command, so in this open window I have a button that takes the value of two input fields hidden in while in the database. When clicking on it, it has a function where it sends the value of this hidden field to another input of the page where the window.open is executed (for simplicity, the x.php page opens via window.open the page y.php, then on page y .php has a button that sends a value returned from the database to the input field in x.php).

Then the problem starts, when I click I can send one input to the other, only the first record listed in the database, the others do not send.

The part of y.php where it does this function follows:

These are the hidden fields:

$query = "SELECT * FROM equipamentos WHERE marker_id LIKE '$id'";


 $result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)){

  <td>".$row['id']."</td>
  <td>".$row['nome']."</td>

<td style=\"text-align:center;\">".$row['lat']."></td>
<td style=\"text-align:center;\">".$row['lng']."</td>
<td style=\"text-align:center;\">
  <input type=\"hidden\" name=\"latMap\" id=\"latMap\" value ='".$row['lat'] ."'></input>
<input type=\"hidden\" name=\"lngMap\" id=\"lngMap\" value ='". $row['lng'] ."'></input>";
</td>

Then I have the submit button:

<button type=\"button\" id=\"btnLocalizar\" name=\"btnLocalizar\" class=\"btn btn-sm btn-primary\">
                            <i class=\"fas fa-map-marker-alt\"></i></button>

And the script to send:

    <script>
  var input1 = document.getElementById('latMap').value;
  var input2 = document.getElementById('lngMap').value;

  $(document).ready(function(){
    $('#btnLocalizar').click(function(e){
      e.preventDefault();
      var input_value = $('#latMap').val();
      $(window.opener.document)

        .find('#latMap')
        .val(input_value)
    });
  });
  </script>

The only problem is just being able to send the first record, if anyone knows how to get all the records listed by the database query, it would be a great help.

    
asked by anonymous 10.04.2018 / 14:48

1 answer

1

There are some problems with the code, such as duplication of id s. The window button will always send the first% of% found. Either you remove the id s or put a variable that differentiates id s, such as an increment variable in id :

$x = 0;
while(){
   ...
   <input type=\"hidden\" name=\"latMap\" id=\"latMap".$x."\" value ='".$row['lat'] ."'></input>
   <input type=\"hidden\" name=\"lngMap\" id=\"lngMap".$x."\" value ='". $row['lng'] ."'></input>";
   ...
   $x++;
}
  

If you were using% s of% s just to capture the elements, you would not   you will need them and you can remove them. Same thing with while and id of buttons within name .

Another thing is these two lines:

var input1 = document.getElementById('latMap').value;
var input2 = document.getElementById('lngMap').value;

They should be useless, so you can remove them too.

id is also useless, as while s is and not submit . You can remove this line too.

Since the buttons are in the same row as the e.preventDefault(); , you can get the click event from the button class, capturing the value of input by .btn . So your values shipping code would look like this:

$(document).ready(function(){
   $('.btn').click(function(){
      var linha = $(this).closest('tr');
      var input_valueLat = linha.find('[name="latMap"]').val();
      var input_valueMap = linha.find('[name="lngMap"]').val();
      $(window.opener.document)
      .find('#latMap')
      .val(input_valueLat);
      $(window.opener.document)
      .find('#lngMap')
      .val(input_valueMap);
   });
});
    
10.04.2018 / 16:00