首页 > 解决方案 > PHP 回显序列中的 JavaScript 不正确

问题描述

我有当前的问题,我的 php 脚本需要一些 JS 代码。它在 PHP 回显中声明。但是,当页面加载完成时,所有“任务”都会在加载时立即触发。

因此,在加载 php 后,当它们符合要求时,我会弹出所有 $messages。示例:在最后一个 php alinea 中,状态:“Goedgekeurd door klant”将被更新到数据库中。但是之后我会在 IF (first alinea) 中声明弹出窗口

我希望脚本从上到下运行。(就像 php 的其余部分正在做的那样)

在任何评论之前,是的,我知道这个脚本很糟糕,并且可以注入。但这只是一个测试场景。在实际代码中,情况并非如此。

这是我的代码:

<?php
if($status == "Goedgekeurd door klant"){
    $message = "Offerte $oid is reeds beantwoord met de status: Goedgekeurd door klant.";
    echo "<script type='module'>alert('$message');
    window.location.href = 'customer_quote.php?offerteid=$offerteid&comp=$comp&ikey=$ikey&fail=1';
    </script>";
}

if($status == "Afgewezen door klant"){
    $message = "Offerte $oid is reeds beantwoord met de status: Afgewezen door klant.";
    echo "<script type='module'>alert('$message');
    window.location.href = 'customer_quote.php?offerteid=$offerteid&comp=$comp&ikey=$ikey&fail=1';
    </script>";
}

//getting customerid from url
$id = $_GET['id'];
$omschrijving = "$klantnaam heeft offerte $oid goedgekeurd.";
$date = date("d-m-Y G:i");
$link1 = "/modules/quotes/offerte.php?offerteid=$offerteid";
 

//Connect to DB
$companyname = $_GET['comp'];
include("$root/connections/customerdbconnection.php");

// Attempt insert query execution
$sql = "INSERT INTO notifications (klant, omschrijving, link, uid, createdat) VALUES ('$klantnaam', '$omschrijving', '$link1', '$oid', '$date')";
if(mysqli_query($link, $sql)){
    echo "Notificatie gecreeerd!";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
 
// Attempt insert query execution
$sql = "UPDATE offertes SET status='Goedgekeurd door klant' WHERE offerteid=$offerteid";
if(mysqli_query($link, $sql)){
    $message = "Dankuwel, offerte $oid is door u gemarkeerd als 'Goedgekeurd'.";
    echo "<script type='module'>alert('$message');
    window.location.href = 'customer_quote.php?offerteid=$offerteid&comp=$comp&ikey=$ikey&fail=1';
    </script>"; 
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>

标签: javascriptphphtml

解决方案


exit();通过将其放在JS 脚本的末尾来修复它。


推荐阅读