Stanislas Piotrowski
Stanislas Piotrowski

Reputation: 2694

Why does Javascript execution do not stop PHP execution?

I met a trouble with a script I have done.

Here is the entire script. But the trouble concern only 2 lines.

In fact I have a PHP condition if it is respected it should display an alert if not, it should execute the following script.

and at the end I have a header location that makes the user go back to the previous page.

The thing is that it does not display the alert, it directly make the user go back. I would like to make the user click on 'ok' in order to make the script continue. Is there a way I can do that.?

<?php
$sql="SELECT SUM(montant) as montantfactures, n_doss
      FROM  `factures` 
      WHERE `n_doss`='".mysql_real_escape_string($_GET['n_doss'])."'
      GROUP BY `n_doss`";

$result=mysql_query($sql) or die;
while($data=mysql_fetch_assoc($result))
{
    $rqt="SELECT `interets`.*,
                 `les_taux`.*,
                 `decompte`.`principal_s`
          FROM   `interets`
          INNER JOIN `les_taux` ON `interets`.`type`=`les_taux`.`type`
          INNER JOIN `decompte` ON `interets`.`n_doss`=`decompte`.`n_doss`
          WHERE `interets`.`n_doss`='".$data['n_doss']."' 
          GROUP BY `date_modif` DESC
          LIMIT 1";
    $resultat=mysql_query($rqt) or die;
    $donnees=mysql_fetch_assoc($resultat);

    {$datefin=$donnees['date_fin'];}
    $datedepart=$donnees['date_depart'];
    if($donnees['date_fin']==date('Y-m-d'))
    {
        echo "<script type=\"text/javascript\">alert(\"les interets sont deja a jour\")</script>";
    }
    else
    {
        if($donnees['type']=='tc')
        {

            if($donnees['montant']==0)
            {

                $taux=($donnees['valeur']*1);
                $nbjours=round((strtotime($datefin) - strtotime($datedepart))/(60*60*24)-1);
                $interets=(($data['montantfactures']-$donnees['principal_s'])*$nbjours*$taux)/(365*100);
                $nouveauinterets=$donnees['montant']+$interets ;
                $itc="UPDATE `interets` SET
                    montant     =   '".$nouveauinterets."',
                    date_fin= NOW() WHERE n_doss='".$donnees['n_doss']."'
                    ";
                $rtq    =   mysql_query($itc) or die(__LINE__.mysql_error().$itc);
                echo "<img src='images/tick_48.png' width='24'> Int&eacute;r&ecirc;ts correctement mis &agrave; jour pour le dossier n&deg;".$donnees['n_doss'].'<br>' ;


            }

            else
            {

                $taux=($donnees['valeur']*1);
                $nbjours=round((strtotime($datefin) - strtotime($datedepart))/(60*60*24)-1);
                $interets=(($data['montantfactures']-$donnees['principal_s'])*1*$taux)/(365*100);
                $nouveauinterets=$donnees['montant']+$interets ;
                $itc="UPDATE `interets` SET
                    montant     =   '".$nouveauinterets."',
                    date_fin= NOW() WHERE n_doss='".$donnees['n_doss']."'";
                $rtq    =   mysql_query($itc) or die(__LINE__.mysql_error().$itc);
                echo "<img src='images/tick_48.png' width='24'> Int&eacute;r&ecirc;ts correctement mis &agrave; jour pour le dossier n&deg;".$donnees['n_doss'].'<br>' ;

            }
        }
        elseif($donnees['type']=='tleg')
        {

            if($donnees['montant']==0)
            {
                if($donnees['operateur']=="x")
                {
                    $taux=($donnees['valeur']*$donnees['variable']);
                    $nbjours=round((strtotime($datefin) - strtotime($datedepart))/(60*60*24)-1);
                    $interets=(($data['montantfactures']-$donnees['principal_s'])*$nbjours*$taux)/(365*100);
                    $nouveauinterets=$donnees['montant']+$interets ;
                    $itc="UPDATE `interets` SET
                    montant     =   '".$nouveauinterets."',
                    date_fin= NOW() WHERE n_doss='".$donnees['n_doss']."'";
                    $rtq    =   mysql_query($itc) or die(__LINE__.mysql_error().$itc);
                    echo "<img src='images/tick_48.png' width='24'> Int&eacute;r&ecirc;ts correctement mis &agrave; jour pour le dossier n&deg;".$donnees['n_doss'].'<br>' ;
                }
                elseif($donnees['operateur']=="+")
                {
                    $taux=($donnees['valeur']+$donnees['variable']);
                    $nbjours=round((strtotime($datefin) - strtotime($datedepart))/(60*60*24)-1);
                    $interets=(($data['montantfactures']-$donnees['principal_s'])*$nbjours*$taux)/(365*100);
                    $nouveauinterets=$donnees['montant']+$interets ;
                    $itc="UPDATE `interets` SET
                    montant     =   '".$nouveauinterets."',
                    date_fin= NOW() WHERE n_doss='".$donnees['n_doss']."'";
                    $rtq    =   mysql_query($itc) or die(__LINE__.mysql_error().$itc);
                    echo "<img src='images/tick_48.png' width='24'> Int&eacute;r&ecirc;ts correctement mis &agrave; jour pour le dossier n&deg;".$donnees['n_doss'].'<br>' ;
                }
            }
            else
            {
                if($donnees['operateur']=="x")
                {
                    $taux=($donnees['valeur']*$donnees['variable']);
                    $nbjours=round((strtotime($datefin) - strtotime($datedepart))/(60*60*24)-1);
                    $interets=(($data['montantfactures']-$donnees['principal_s'])*1*$taux)/(365*100);
                    $nouveauinterets=$donnees['montant']+$interets ;
                    $itc="UPDATE `interets` SET
                    montant     =   '".$nouveauinterets."',
                    date_fin= NOW() WHERE n_doss='".$donnees['n_doss']."'";
                    $rtq    =   mysql_query($itc) or die(__LINE__.mysql_error().$itc);
                    echo "<img src='images/tick_48.png' width='24'> Int&eacute;r&ecirc;ts correctement mis &agrave; jour pour le dossier n&deg;".$donnees['n_doss'].'<br>' ;
                }
                elseif($donnees['operateur']=="+")
                {
                    $taux=($donnees['valeur']+$donnees['variable']);
                    $nbjours=round((strtotime($datefin) - strtotime($datedepart))/(60*60*24)-1);
                    $interets=(($data['montantfactures']-$donnees['principal_s'])*1*$taux)/(365*100);
                    $nouveauinterets=$donnees['montant']+$interets ;
                    $itc="UPDATE `interets` SET
                    montant     =   '".$nouveauinterets."',
                    date_fin= NOW() WHERE n_doss='".$donnees['n_doss']."'";
                    $rtq    =   mysql_query($itc) or die(__LINE__.mysql_error().$itc);
                    echo "<img src='images/tick_48.png' width='24'> Int&eacute;r&ecirc;ts correctement mis &agrave; jour pour le dossier n&deg;".$donnees['n_doss'].'<br>' ;

                }
            }
        }
        elseif($donnees['type']=='tbce')
        {

            if($data['montantfactures']==0)
            {
                if($donnees['operateur']=="x")
                {
                    $taux=($donnees['valeur']*$donnees['variable']);
                    $nbjours=round((strtotime($datefin) - strtotime($datedepart))/(60*60*24)-1);
                    $interets=(($data['montantfactures']-$donnees['principal_s'])*$nbjours*$taux)/(365*100);
                    $nouveauinterets=$donnees['montant']+$interets ;
                    $itc="UPDATE `interets` SET
                    montant     =   '".$nouveauinterets."',
                    date_fin= NOW() WHERE n_doss='".$donnees['n_doss']."'";
                    $rtq    =   mysql_query($itc) or die(__LINE__.mysql_error().$itc);
                    echo "<img src='images/tick_48.png' width='24'> Int&eacute;r&ecirc;ts correctement mis &agrave; jour pour le dossier n&deg;".$donnees['n_doss'].'<br>' ;
                }
                elseif($donnees['operateur']=="+")
                {
                    $taux=($donnees['valeur']+$donnees['variable']);
                    $nbjours=round((strtotime($datefin) - strtotime($datedepart))/(60*60*24)-1);
                    $interets=(($data['montantfactures']-$donnees['principal_s'])*$nbjours*$taux)/(365*100);
                    $nouveauinterets=$donnees['montant']+$interets ;
                    $itc="UPDATE `interets` SET
                    montant     =   '".$nouveauinterets."',
                    date_fin= NOW() WHERE n_doss='".$donnees['n_doss']."'";
                    $rtq    =   mysql_query($itc) or die(__LINE__.mysql_error().$itc);
                    echo "<img src='images/tick_48.png' width='24'> Int&eacute;r&ecirc;ts correctement mis &agrave; jour pour le dossier n&deg;".$donnees['n_doss'].'<br>' ;
                }
            }
            else
            {

                if($donnees['operateur']=="x")
                {
                    $taux=($donnees['valeur']*$donnees['variable']);
                    $nbjours=round((strtotime($datefin) - strtotime($datedepart))/(60*60*24)-1);
                    $interets=(($data['montantfactures']-$donnees['principal_s'])*1*$taux)/(365*100);
                    $nouveauinterets=$donnees['montant']+$interets ;
                    $itc="UPDATE `interets` SET
                    montant     =   '".$nouveauinterets."',
                    date_fin= NOW() WHERE n_doss='".$donnees['n_doss']."'";
                    $rtq    =   mysql_query($itc) or die(__LINE__.mysql_error().$itc);
                    echo "<img src='images/tick_48.png' width='24'> Int&eacute;r&ecirc;ts correctement mis &agrave; jour pour le dossier n&deg;".$donnees['n_doss'].'<br>' ;
                }
                elseif($donnees['operateur']=="+")
                {
                    $taux=($donnees['valeur']+$donnees['variable']);
                    $nbjours=round((strtotime($datefin) - strtotime($datedepart))/(60*60*24)-1);
                    $interets=(($data['montantfactures']-$donnees['principal_s'])*1*$taux)/(365*100);
                    $nouveauinterets=$donnees['montant']+$interets ;
                    $itc="UPDATE `interets` SET
                    montant     =   '".$nouveauinterets."',
                    date_fin= NOW() WHERE n_doss='".$donnees['n_doss']."'";
                    $rtq    =   mysql_query($itc) or die(__LINE__.mysql_error().$itc);
                    echo "<img src='images/tick_48.png' width='24'> Int&eacute;r&ecirc;ts correctement mis &agrave; jour pour le dossier n&deg;".$donnees['n_doss'].'<br>' ;
                }
            }
        }
    }
}
header('Location: ' . $_SERVER['HTTP_REFERER'] );
?>

Upvotes: 0

Views: 1036

Answers (1)

Sebastian Breit
Sebastian Breit

Reputation: 6159

Javascript can't stop php execution! javascript runs on the client side (browser) and php runs in the server. if you write header('Location: ' . $_SERVER['HTTP_REFERER'] ); , it will always redirect you there.

If I was you, I would modify this line and add this:

echo "<script type=\"text/javascript\">alert(\"les interets sont deja a jour\"); window.location = $_SERVER['HTTP_REFERER']
</script>"; 

and remove the header location, of course..

Upvotes: 1

Related Questions