0

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'] );
?>
6
  • Which 2 lines? It would be way easier, if you could provide a more minimalistic version of your script, with everything unrelated removed from it! Commented Oct 8, 2012 at 13:40
  • which line is the issue on? The if(donnees['date-fin']==date('Y-m-d')) line? Commented Oct 8, 2012 at 13:41
  • I'm sorry the first line is the following with the javascript if($donnees['date_fin']==date('Y-m-d')) { echo "<script type=\"text/javascript\">alert(\"les interets sont deja a jour\")</script>"; } and the last contain the header location at the end Commented Oct 8, 2012 at 13:42
  • Your script will never produce any meaningful result, it will immediately redirect the user regardless of what you do. Are you even familiar with the way PHP / JS and browsers work? Judging by the code - you are a beginner (no insult intended) and you should probably learn what headers are. Commented Oct 8, 2012 at 13:43
  • 1
    The header('Location:'); is what stops your script from producing any output. The way it works is this: you ask for something from the server and server replies. It sends the headers to notify your browser what happened (resource found, not found, moved etc) and it sends the response (the thing you tried to echo). Since headers are parsed first, the browser will notice the Location header and it will redirect you because it won't try to present the content (in your case - the JS with the alert in it). Commented Oct 8, 2012 at 13:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Ok so you mean there is no way to make a kind of temporisation?
yes, you can do it. If I understood well, you want the user to be redirected after the alert, right? I edited my answer ;)
Yes After the alert, but if there is no alert, after the execution of the script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.