2015-07-11 75 views
2

考慮這個圖像,當用戶點擊一個鏈接時這是一個iframe窗口。提交表單時保持表單窗口打開

enter image description here

我的問題

當用戶點擊存款的形式被提交和窗口關閉,因此用戶不知道,如果存款是成功還是失敗。

我想做

我正在尋找一種方式來什麼保持iframe的窗口打開形式提交後,顯示相應的消息

表HTML

<form name="depForm" action="" id="register_form" method="post"> 
      User Name<br /><input type="text" name="uname" value="" /><br /> 
      Credit Card Nr<br /><input type="text" name="cc" value="" /><br /> 
      CSV Nr<br /><input type="text" name="csv" value="" /><br /> 
      Amount<br /> <input type="text" name="amount" value="" /><br /> 
      <input type="submit" value="deposit" name="deposit" class="buttono" /> 
      </form> 

PHP代碼

if(isset($_POST['deposit'])){ 
      if(isset($_SESSION['FBID'])){ 
       $uid=$_SESSION['FBID']; 
       $amount = $_POST['amount']; 
       $cc = $_POST['cc']; 
       $csv = $_POST['csv']; 
       //new bal 
       $bal = getBal($uid); 
       $newBal = $bal+$amount; 
       $sql="UPDATE members SET balance='$newBal' WHERE member_nr='$uid'"; 
       $result = mysql_query($sql) or die("error please try again"); 
        if($result){ 

        } 
      } 

如果有人可以告訴我如何保持iframe在表單提交後打開它將不勝感激。

+1

你需要改變表單提交使用AJAX。在響應中,您可以包含一個狀態標誌以向UI指示請求是否成功並且適當地執行。 –

+0

@RoryMcCrossan是的,我認爲它需要使用AJAX(嘆氣)不知道多少關於Ajax ,,,猜測它回到了我的教程 – Marilee

+0

我給你添加了一個答案。 –

回答

3

您需要更改表單提交以使用AJAX。在響應中,您可以包含一個狀態標誌以向UI指示請求是否成功並且適當地執行。事情是這樣的:

$('#register_form').submit(function(e) { 
    e.preventDefault(); // stop the standard form submission 
    $.ajax({ 
     url: this.action, 
     type: this.method, 
     data: $(this).serialize(), 
     success: function(data) { 
      if (data.success) { 
       // show success message in UI and/or hide modal 
      } else { 
       // it didn't work 
      } 
     }, 
     error: function(xhr, status, error) { 
      // something went wrong with the server. diagnose with the above properties 
     } 
    }); 
}); 
$success = false; 
if (isset($_POST['deposit'])) { 
    if (isset($_SESSION['FBID'])) { 
     $uid = $_SESSION['FBID']; 
     $amount = $_POST['amount']; 
     $cc = $_POST['cc']; 
     $csv = $_POST['csv']; 

     $bal = getBal($uid); 
     $newBal = $bal + $amount; 
     $sql = "UPDATE members SET balance='$newBal' WHERE member_nr='$uid'"; 
     $result = mysql_query($sql) or die("error please try again"); 
     if ($result) { 
      $success = true; 
     } 
    } 
} 

echo json_encode(array('success' => $success)); 
+0

非常感謝你的努力,希望我能做的不僅僅是簡單地給你一個贊成票,我真的很感謝 – Marilee

+0

林不好意思問你這麼一個愚蠢的問題,但我的Ajax知識是不存在的......我應該在哪裏插入代碼的第一部分... ajax部分? – Marilee

+0

在jQuery文檔就緒處理程序中。如果你不熟悉jQuery,[這裏是一個指南](https://learn.jquery.com/about-jquery/how-jquery-works/) –

相關問題