2013-10-05 66 views
0

這是我近一週的問題,我無法解決它。Javascript:當按下CANCEL按鈕仍然保存數據

場景:

之前,我保存的數據將顯示一個確認消息,說... Station is full. do you want to continue saving? ...然後當我試着取消它,它仍然保存數據..

按CANCEL按鈕,它也救不了了....

假設我有一個大廈名稱:星際1和星際2 ...

「星際1」「星2」將是值$ bldg ..然後總持續時間爲星1是08:00:00 [這已滿],然後爲星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 
    } 

所以,當我打的取消將只是去else of do nothing ...但它仍然保存。基於

我的理解...... STAR 1將會第一個閱讀for循環的程序,然後它會去"if($network == "Mass and Mobile" && $row[fldTotalDuration] == "08:00:00" && $duration >= "0 s")"然後彈出消息將出現,然後我將點擊取消按鈕,然後$cancel的值已經是「是」,那麼STAR 2已經保存在我的數據庫是應該停止程序的保存取消單擊時....

感謝..

+0

$ cancel的值來自哪裏?在第一行中,檢查是否爲「是」,但不顯示如何設置。 – echosa

+0

@ echosa..sorry我忘了包括那個...我編輯我的文章... – user2826499

+1

做你的第一個如果陳述$ _GET的評估。我通常做一個alert();你也沒有使用{}。因此,清理if,並對isset()返回的內容進行警報。我會將它匹配到一個值,而不僅僅是密鑰的存在。 –

回答

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

這只是一點點的代碼,但並不容易出錯。