0

i have a db records echoed out and formatted in a table with a delete link(icon) on each table row, indexed with a while loop as the data gets fetched from db. On clicking the delete icon, i expect the particular indexed row to get deleted from db and also from the table . Here are my codes:

<script>
function submitForm(){
document.getElementById("myform").submit()
}</script>
</head>
<body>
<table>
<tbody>

 <?php 

$rowIndex = 0;
while($row = mysqli_fetch_row($result){
 $user = $row[0];
 $address=$row[1];
 $phone = $row[2];
echo '<tr>
<td>'.$user.'</td>
<td>'.$address.'</td>
<td>'.$phone.'</td>
<td><a href="" onclick="javascript: submitForm()">Delete(icon)</a>
</tr>'
$rowIndex+=1;

}
?>
</tbody>
</table>
<form method ="post" id ="myform" action ="test.php" >
<input type="hidden" name="del">
</form>

on clicking the 'Delete' Link , i expect the javascript to submit the form and move to 'test.php'(though i intend to remain on same form, but that way i will know my form ACTUALLY got submitted). Please any help will be appreciated.

vhu
  • 12,244
  • 11
  • 38
  • 48
Jay
  • 53
  • 9
  • 1
    Do you get any errors in your browser console? Have you tried `onclick="submitForm()"`? Have you tried using a ` – brombeer May 27 '20 at 05:13
  • Try `onclick="submitForm(); return false"` – Phil May 27 '20 at 05:19
  • Tnx Phil! please can u explain why the return false. That alone made the code work! please alittle explanation and i will vote as answer to this qxn – Jay May 27 '20 at 06:29
  • `return false` stops the link from navigating to its `href` attribute value – Phil May 27 '20 at 06:33

1 Answers1

0
<!DOCTYPE html>
<html>
<head>
<title>test javascript form test </title>
</head>
<body>
<form action="#" method="post">
<label>Username</label>
<input type="text" id="name"><br>
<label>Password</label>
<input type="password" id="password"><br>
<button onclick="data()" type="button">Submit</button>
</form>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script>
function data(){
   var name = $("#name").val();
   var pass = $("#password").val();
   alert(name);
   alert(pass);
}
</script>
</body>
</html>