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

?>

Link to comment
Share on other sites

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.
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.