Peter Peter - 1 year ago 65
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.

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">

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


<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']; ?> >

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


Here is javascript:

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

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download