这是我的问题,差不多一个星期,我无法解决。
情景:
在我保存数据之前,会出现一条确认消息,说明...... Station is full. do you want to continue saving?
...然后当我尝试取消它时,它仍然保存数据..
按下CANCEL按钮,它将不再保存....
假设我有一个建筑物名称:Star 1和Star 2 ......
“Star 1”,“Star 2”将是 $ bldg 的值...然后是<的持续时间< em> STAR 1是08:00:00 [已经满了] ,然后 STAR 2是00:00:00 [数据库中尚不存在]这是我的代码:
if (isset($_GET['js_varOK']))
$cancel = $_GET['js_varOK'];
else $cancel = "not";
if($cancel != "yes")
{
for($i=0; $i<sizeof($bldg);$i++)
{
if($cancel != "yes")
{
$result = mysql_query("SELECT * FROM tbldata WHERE fldNetname = '".$network."' AND fldBldgName = '".$bldg[$i]."' AND fldWeek = '".$week."' AND fldMonth = '".$month."' AND fldYear ='".$year."' ORDER BY id desc");
if(mysql_num_rows($result)==0)
{
$totalduration = "";
$seconds = 0;
$minutes = 0;
$computeSecMin = $seconds * $minutes;
$subSecMin = $computeSecMin + $duration;
$getMin = floor($subSecMin/60);
$getSec = $subSecMin%60;
$totalduration = $getMin .":". $getSec;
$query2 = "INSERT INTO tbldata(fldNetname,fldBldgName,fldPlaylist,fldMonth,fldWeek,fldYear,fldDuration,fldFrom,fldTo,fldTotalDuration,fldTimestamp,fldStatus, fldUser) VALUES ('".$network."','".$bldg[$i]."','".$AdName."','".$month."','".$week."','".$year."','".$duration."','".$from."','".$to."', '".$totalduration."','".$date."','".$status."',(SELECT fldid from tblmembers where fldUsername= '".$_SESSION['user']."'))";
mysql_query($query2) or die (mysql_error());
echo "<script type='text/javascript'>\n";
echo "alert('Data have been saved first!');\n";
echo "</script>";
}
else
{
while ($row = mysql_fetch_array($result))
{
if($network == "Mass and Mobile" && $row[fldTotalDuration] == "08:00:00" && $duration >= "0 s")
{?><?php
if(sizeof($bldg) == 1)
{
echo "<script type='text/javascript'>\n";
echo "alert('$bldg[$i] station is already full.');\n";
echo "window.location='booking_content.php'";
echo "</script>";
}
else
{ ?>
<script>
var myVar = "station is already full. Do you want to save the other networks?";
var js_varOK;
var bldg;
var network;
if (confirm(myVar))
{
js_varOK = "no";
bldg = "<?php echo $bldg?>";
network= "<?php echo $network?>";
window.location = "?js_varOK=" + js_varOK + "&bldg="+bldg + "&network="+network;
} else
{
js_varOK = "yes";
window.location = "?js_varOK=" + js_varOK;
}
</script>
<?php
}
}
}
}
else
{
//do noothing
}
所以,当我点击CANCEL时,它只会转到else of do nothing
...但它仍在保存..
根据我的理解...... STAR 1将是第一个读取for循环的程序,然后它将转到"if($network == "Mass and Mobile" && $row[fldTotalDuration] == "08:00:00" && $duration >= "0 s")"
然后会出现一个弹出消息然后我会点击取消按钮然后$cancel
的值已经是“是”,那么STAR 2已经保存在我的数据库中,应该停止程序在取消点击时保存它....
感谢..
答案 0 :(得分:0)
在两种情况下都设置了变量$_GET['js_varOK']
。如果你不想保存的话,它只是空的。
尝试这样证明:
if (isset($_GET['js_varOK']) && $_GET['js_varOK'] != '') { // better way: .. && $_GET['js_varOK'] == 'yes'
echo 'save';
} else {
echo 'dont save';
}
我建议稍微清理你的代码。使用
if ($a == $b) {
echo 'yes';
} else {
echo 'no';
}
而不是
if ($a == $b) echo 'yes';
else echo 'no';
这只是一点点代码,但不容易出错。