rguitar Posted March 3, 2013 Share Posted March 3, 2013 I have a table built from MySql database by PHP and I want to have the ability to delete a row by a delete button at the end of each row. When clicking the Cancel on confirm it continues to proceed with php code to delete the row. Could there be a problem with my $_GET['id'] and form method of POST? I thought I could mix them. I am trying to get the row id from the querystring to delete that row from database. my php: if(isset($_GET['id'])) { $stmt = $conn->stmt_init(); $sqlDelete = "delete from Brucella where Id = ?"; if($stmt->prepare($sqlDelete)){ $stmt->bind_param('i', $_GET['id']); $stmt->execute(); if ($stmt->affected_rows > 0 ) { header('Location: http://www.petlabsdiagnostics.com/brucella.php'); } my html: <form id="hemo" name="hemo" method="post" action=""> <table cellpadding="5" width="900" style="border-bottom:solid #000000 1px"> <tr> <td width="66"><a href="employee_workspace.php">Workspace</a></td> <td width="5">|</td> <td width="40"><a href="equine_test_list.php">Equine</a></td> <td width="5">|</td> <td width="40"><a href="feline_test_list.php">Feline</a></td> <td width="5">|</td> <td width="40"><a href="canine_test_list.php">Canine</a></td> <td width="5">|</td> <td width="40"><a href="canine_test_list.php">Canine</a></td> <td width="5">|</td> <td width="40"><a href="Library/brucella.php">Brucella</a></td> <td width="410" style="font-size:12px; text-align:right"><a href="logout.inc.php">Logout</a></td> </tr> </table> <div id = "mainContent"> <h2 class="title">Batch Brucella Canis Results</h2> <div id="client_info"> <br /> <table id="tb_header" cellpadding="1" cellspacing="2" width="700px" > <tr> <td width="175"><strong>Client Name</strong></td> <td width="175"><strong>Patient Name</strong></td> <td width="50"><strong>ID#</strong></td> <td width="50"><strong>Age</strong></td> <td width="50"><strong>Sex</strong></td> <td width="150"><strong>Doctor</strong></td> <td width="50"><strong>Results (pos/neg)</strong></td> </tr> <tr><td><input type="text" name="txt_client_name" id="txt_client_name" /></td> <td><input type="text" name="txt_patient_name" id="txt_patient_name"/></td> <td><input type="text" name="txt_id" id="txt_id" size="12" /></td> <td><input type="text" name="txt_age" id="txt_age" size="8" /></td> <td><select name="sel_sex"> <option value="">Select</option> <option value="Male">Male</option> <option value="Female">Female</option> </select></td> <td><input type="text" name="txt_doctor" id="txt_doctor" /></td> <td><select name="sel_results"> <option value="">Select</option> <option value="POS">POS</option> <option value="NEG">NEG</option> </select></td><td> <input type="submit" value="Submit" id="submit" class="btn" name="submit" /></td> </tr> <tr><td> </td></tr> <?php for($i = 0; $i < $numRows ; $i++) { $row = $result->fetch_assoc(); if($i % 2) { $RowColor="style='background-color:#ede4c5; border:solid 1px black;'"; } else { //$RowColor="style='background-color:#c2c290'"; $RowColor="style='background-color:#ffffff;border:solid 1px black;'"; }?> <tr><td <?php echo $RowColor ?>><?php echo $row['ClientName'] ?></td><td <?php echo $RowColor ?>><?php echo $row['PatientName'] ?></td><td <?php echo $RowColor ?>><?php echo $row['IDNumber'] ?></td><td <?php echo $RowColor ?>> <?php echo $row['Age'] ?></td><td <?php echo $RowColor ?>><?php echo $row['Sex'] ?></td><td <?php echo $RowColor ?>><?php echo $row['Doctor'] ?></td><td <?php echo $RowColor ?>><?php echo $row['Results'] ?></td><td><a href="brucella.php?id=<?php echo $row['Id'] ?>" onclick="warning();">Delete</a></td></tr> <?php } ?> my javascript: <script type="text/javascript"> function warning() { var r = confirm("Are you sure?"); if(r == true) { // do something return true; } else { // do something return false; } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/275187-javascript-confirm-will-not-stop-delete-on-my-php-code/ Share on other sites More sharing options...
Jessica Posted March 3, 2013 Share Posted March 3, 2013 Since clicking the link doesn't submit a form, it doesn't matter, you are using $_GET for the link because you have ?id= and the id, and in your code you try to get $_GET['id']. So it looks fine. The problem is with the javascript, it doesn't seem to be actually cancelling the browser from following the link. I'm going to move this to the Javascript forum for you. I've never tried to do anything like that so I can't help. Quote Link to comment https://forums.phpfreaks.com/topic/275187-javascript-confirm-will-not-stop-delete-on-my-php-code/#findComment-1416284 Share on other sites More sharing options...
rguitar Posted March 3, 2013 Author Share Posted March 3, 2013 ok, thanks for your quick reply. Quote Link to comment https://forums.phpfreaks.com/topic/275187-javascript-confirm-will-not-stop-delete-on-my-php-code/#findComment-1416288 Share on other sites More sharing options...
Solution teynon Posted March 3, 2013 Solution Share Posted March 3, 2013 (edited) Add a return in the onclick. <tr><td <?php echo $RowColor ?>><?php echo $row['ClientName'] ?></td><td <?php echo $RowColor ?>><?php echo $row['PatientName'] ?></td><td <?php echo $RowColor ?>><?php echo $row['IDNumber'] ?></td><td <?php echo $RowColor ?>> <?php echo $row['Age'] ?></td><td <?php echo $RowColor ?>><?php echo $row['Sex'] ?></td><td <?php echo $RowColor ?>><?php echo $row['Doctor'] ?></td><td <?php echo $RowColor ?>><?php echo $row['Results'] ?></td><td><a href="brucella.php?id=<?php echo $row['Id'] ?>" onclick="return warning();">Delete</a></td></tr> Edited March 3, 2013 by teynon Quote Link to comment https://forums.phpfreaks.com/topic/275187-javascript-confirm-will-not-stop-delete-on-my-php-code/#findComment-1416300 Share on other sites More sharing options...
Jessica Posted March 3, 2013 Share Posted March 3, 2013 That doesn't sound right to me... But I always use jquery. Quote Link to comment https://forums.phpfreaks.com/topic/275187-javascript-confirm-will-not-stop-delete-on-my-php-code/#findComment-1416313 Share on other sites More sharing options...
teynon Posted March 4, 2013 Share Posted March 4, 2013 (edited) It would be the same case in jQuery, but using a different method. (I prefer jQuery as well.) But it was easier to recommend adding return here for simplicity's sake. If you were going to do it with jQuery, it would be something like this: $('#object').on("click", function() { if (confirm("Are you an oompa loompa?")) { return true; } return false; }); Personally, I wouldn't use an anchor tag ("<a>") in this case. I would probably use a button. Edited March 4, 2013 by teynon Quote Link to comment https://forums.phpfreaks.com/topic/275187-javascript-confirm-will-not-stop-delete-on-my-php-code/#findComment-1416315 Share on other sites More sharing options...
Jessica Posted March 4, 2013 Share Posted March 4, 2013 I've just never seen a return in an onclick - I'm not doubting you, if it worked than it worked. It sort of makes sense now that I think about it. It's returning what was returned by the function, which is what threw me off. Quote Link to comment https://forums.phpfreaks.com/topic/275187-javascript-confirm-will-not-stop-delete-on-my-php-code/#findComment-1416320 Share on other sites More sharing options...
haku Posted March 4, 2013 Share Posted March 4, 2013 (edited) Returning true in an onclick means the link is followed, and returning false means that the link will not be followed (ie - nothing happens). function warning() { return confirm("Are you sure?"); } This is all you need. For that matter, you could even use this without the javascript function at all: <a href="brucella.php?id=<?php echo $row['Id'] ?>" onclick="return confirm("Are you sure?");">Delete</a> But that said, I really don't like inline javascript, so I would have done it differently right from the beginning. Edited March 4, 2013 by haku Quote Link to comment https://forums.phpfreaks.com/topic/275187-javascript-confirm-will-not-stop-delete-on-my-php-code/#findComment-1416339 Share on other sites More sharing options...
rguitar Posted March 4, 2013 Author Share Posted March 4, 2013 thanks for all replies. I tested the code and it seems it only works if the javascript function returns true or false using the return statement in the function and using the return statement after onclick event. Although the function does respond to 'ok' and 'cancel' buttons (tested by using alert boxes) properly, the return statement is needed to prevent the redirect on 'cancel' Quote Link to comment https://forums.phpfreaks.com/topic/275187-javascript-confirm-will-not-stop-delete-on-my-php-code/#findComment-1416466 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.