R M
R M

Reputation: 317

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.

Upvotes: 1

Views: 6881

Answers (3)

To Fix:

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

by

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

Upvotes: 0

Quijote Shin
Quijote Shin

Reputation: 501

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');
}

Upvotes: 0

MrCode
MrCode

Reputation: 64536

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.

Upvotes: 2

Related Questions