Jump to content

When Return False is added, script doesn't execute


Recommended Posts

I have a form on my page with results and check boxes for each row and a delete button at the bottom. The form onSubmit calls a javascript funtion that confirms the user wants to delete. If okay, they they are pushed to my delete page and everything works fine. So, here's the issue, everything works perfectly if I do not put "return false;" in the javascript. With "return false;" in my javascript, the user is just taken to the delete page and nothing happens, it's blank. No errors, nothing. Also, no records are deleted and they are not forwarded to my header location.

 

Without "return false;" in my js, no matter if "ok" or "cancel" is clicked, the record is deleted. Is there something wrong with my code, am I missing something obvious here?

 

INDEX.PHP (ONLY GIVING YOU FORM SECTION)

 


<form method="post" action="" onSubmit="return removemult();" name="updateleads" id="updateleads">
<table>
<tbody>
    while ($stmt->fetch()) {
echo'<tr>
<td><input type="checkbox" name="checked[]" value="'.$ID.'"></td>
<td><a href="/cms/view/?ID='.$ID.'"><strong>'.$firstname.' '.$lastname.'</strong></a></td>
</tr>';
</tbody>
</table>
<input name="remove" type="submit" value="Delete">
</form>

 

JS

function removemult() {
if (confirm("Are you sure you want to delete these leads?")) {
window.location = '/cms/scripts/delete-mult.php';
  }
return false; // If removed, no matter if ok or cancel is clicked, the rows are deleted. Leave it in and cancel works, 
                     // but if ok is clicked, it goes to /cms/scripts/delete-mult.php but it's just a blank white page, no errors, nothing.
}

 

DELETE-MULT.PHP

<?php
require("/config.php");

// DELETE MULTIPLE ROWS

  if(isset($_POST['remove']) && (isset($_POST['checked']))) {

$checked = $_POST['checked'];
$count = count($_POST['checked']);
	var_dump($checked); 

for($i=0;$i<$count;$i++) {
$del_id = $checked[$i];

$stmt = $mysqli->query("DELETE FROM contacts WHERE ID = '$del_id'");
}
if($stmt) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
else {
echo "Error: (" .$mysqli->connect_errno . ")" . " " . $mysqli->connect_error;
}
}

$mysqli->close();

?>

Try:


<form method="post" action="/cms/scripts/delete-mult.php" onSubmit="return removemult();" name="updateleads" id="updateleads">
<table>
<tbody>
    while ($stmt->fetch()) {
echo'<tr>
<td><input type="checkbox" name="checked[]" value="'.$ID.'"></td>
<td><a href="/cms/view/?ID='.$ID.'"><strong>'.$firstname.' '.$lastname.'</strong></a></td>
</tr>';
</tbody>
</table>
<input name="remove" type="submit" value="Delete">
</form>

 

function removemult() {
if (confirm("Are you sure you want to delete these leads?")) {
return true;
  }
return false; // If removed, no matter if ok or cancel is clicked, the rows are deleted. Leave it in and cancel works, 
                     // but if ok is clicked, it goes to /cms/scripts/delete-mult.php but it's just a blank white page, no errors, nothing.
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.