Peter Peter - 13 days ago 4
Javascript Question

When I try to send id value of row via POST to another page, this page will always GET id value 1

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.

Answer

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(); }); });