简体   繁体   中英

Refresh page after “OK” alert popup with javascript

I tried to refresh the page after deleting an item from my back-end list.

Here's the HTML

    <a href="index.php?id=<?php 
echo $array[id_news]; 
?>&?action=delete" onClick="return conferma()">Remove</a>

Here's the PHP

if ($_POST['action'] = "delete") {
    $sql="DELETE FROM news WHERE id_news=".$_GET['id'];
    if (!mysql_query($sql)) {}  
    }

Here's the Javascript

function conferma() {
    return confirm('Confermi di voler cancellare la news selezionata?');
    window.location.reload();
}

The popup appears but after clicking OK the page don't refresh.

You are returning on the confirm() line, so the reload never gets executed. Change to:

function conferma() {
    if(confirm('Confermi di voler cancellare la news selezionata?')){
        // call the delete script via ajax now.....

        window.location.reload();
    }
    return false;
}

It looks like you need to use AJAX to call the delete script, because otherwise the reload will happen and the anchor's href will never be visited.

You are return the boolean result from confirm dialog action, and then reloading, so the script never reach the reload

function conferma() {
    ritorno = confirm('Confermi di voler cancellare la news selezionata?');

  if(ritorno)
    window.location.reload();
  else console.log('ok nothing to do');
}

To Fix:

if ($_POST['action'] = "delete") {

by

if ($_POST['action'] == "delete") {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM