0

I started learning webdeveloping and i tried to send "id" of one of the rows generated from database to another page. The rows are clickable thanks to the javascript code, so i can choose whichever row i want. The problem is, that even though the POST method seems right:

<form id="send" method="POST" action=<?php echo "secondpage.php?id=". $row['id']; ?> ></form>   

// In inspect of the main page it gets the value.

However second page always receive id value of 1. Doesn't matter if i click on the row with id=18 or any other. It will always recieve value of 1...

I heard that this could be a problem with javascript code which i put under PHP code.

Here is a code with PHP:

<div id="content">
<table id="usersTable" class="table table-bordered table-hover table-sm ">
<form action=http://localhost/dodawanie.php>

    <input class="btn btn-primary" type="submit" value="Dodawanie">

</form>
<?php if ($result->num_rows > 0) {
     while($row = $result->fetch_assoc()) {?>

    <tr>         

        <td><?php echo "id: ". $row['id']; ?> </td> 
    <td><?php echo "Name: ". $row["first_name"]; ?> </td> 
        <td><?php echo "Last: ". $row["last_name"];?>  </td>
    <form id="send" method="POST" action=<?php echo "secondpage.php?id=". $row['id']; ?> >
</form> 
    </tr>

   <?php }
} else {
     echo "0 results";
}

$conn->close();
   ?>
</table>
</div>

Here is javascript:

<script type = "text/javascript">
$(document).ready(function(){
    $('#usersTable').find('tr').click( function(){
    //  alert('You clicked row ' + ($(this).index()+1) );
            $('#send').submit();
        });
    });
</script>

I would gladly accept any help to find an error here.

Peter
  • 3
  • 2

2 Answers2

1

Change the <form id="send" id value as unique

or use a class some thing like below:
<form class="form_send" then in your javascript search for the form_class inside the clicked tr:

$(document).ready(function(){
    $('#usersTable').find('tr').click( function(){
          $(this).find('form.form_send').submit();
        });
    });
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
tradebel123
  • 425
  • 1
  • 5
  • 19
0

Ids have to be unique. $('#send').submit() only finds and submits the first form with that id.

You could add your row id to the form's id attribute to make them unique for example.

Andy
  • 28,587
  • 9
  • 37
  • 58