2017-04-02 29 views
1

我有一個腳本,我想用ajax處理表單數據。該腳本正在返回成功消息,但不返回錯誤消息。看看下面的腳本。PHP AJAX腳本在所有情況下都不能正常工作

AJAX腳本

$(document).ready(function() { 
    $("#submit").click(function() { 
     var dataString = { 
      flip: $("#flip").val(), 
      amount: $("#amount").val() 
     }; 
    $.ajax({ 
      type: "POST", 
      dataType : "json", 
      url: "flip-process.php", 
      data: dataString, 
      cache: true, 
     beforeSend: function(){ 
       $("#submit").hide(); 
       $("#loading").show(); 
       $(".message").hide(); 
     }, 
      success: function(json){ 
       setTimeout(function(){ 
        $(".message").html(json.status).fadeIn(); 
        $('#mywallet').html('$' + json.deduct); 
        $("#submit").show(); 
        $("#loading").hide(); 
       },3000); 
      } 
     }); 
     return false; 
    }); 
}); 

PHP腳本

<?php 
session_start(); 
include'config/db.php'; 
$msg = null; 
$sessionid = (!empty($_SESSION['login']))?$_SESSION['login']:null; 

$wp = $pdo->prepare("SELECT set_cointoss_wp, set_cointoss_prob FROM settings"); 
$wp-> execute(); 
$sp = $wp->fetch(); 
    $percent = $sp['set_cointoss_wp']; 
    $probablity = $sp['set_cointoss_prob']; 

$bal = $pdo->prepare("SELECT mb_acbal, mb_wallet FROM mem_balance WHERE mb_id = :mem"); 
$bal-> bindValue(':mem', $sessionid); 
$bal-> execute(); 
$bf = $bal->fetch(); 
    $balance = $bf['mb_acbal']; 
    $wallet = $bf['mb_wallet']; 

$coin = (!empty($_POST['flip']))?$_POST['flip']:null; 
$amount = (!empty($_POST['amount']))?$_POST['amount']:null; 

if($_POST){ 
     if($wallet < $amount){ 
      $msg = "<div class='message-error'>Sorry buddy! You have insufficient balance. Please <a href=''>recharge</a> your wallet.</div>"; 
     }else{ 
      $deduct = $wallet-$amount; 
      $prob = rand(1, 10); 

      //set new wallet balance after bet amount deduction 
      $stmt = $pdo->prepare("UPDATE mem_balance SET mb_wallet = :bal WHERE mb_user = :user"); 
      $stmt-> bindValue(':bal', $deduct); 
      $stmt-> bindValue(':user', $sessionid); 
      $stmt-> execute(); 

     if($coin == ''){ 
      $msg = "<div class='message-error'>Sorry buddy! Fields cannot be left empty.</div>"; 
     }else{ 
      if($coin == "head"){ 
        if($prob <= $probablity){ 
         $result = 1; 
        }else{ 
         $result = 2; 
        } 
      if($result == 1){ 
       // win 
         $wa = $amount*$percent; 
         $win_amount = $wa/100; 
         $final_cash = $win_amount+$balance; 

         // update database with winning amount 
         $stmt = $pdo->prepare("UPDATE mem_balance SET mb_acbal = :bal WHERE mb_user = :user"); 
         $stmt-> bindValue(':bal', $final_cash); 
         $stmt-> bindValue(':user', $sessionid); 
         $stmt-> execute(); 

       $msg = "<div class='message-success'>Congratulations buddy! You won... <strong>$".$win_amount."</strong> has been credited to your account.</div>"; 
      }else{ 
       // loose 
       $msg = "<div class='message-error'>Sorry buddy! You lost... But do not loose hope. Try your luck again :)</div>"; 
      } 
      }else{ 
        if($prob <= $probablity){ 
         $result = 2; 
        }else{ 
         $result = 1; 
        } 
      if($result == 1){ 
       // loose 
       $msg = "<div class='message-error'>Sorry buddy! You lost... But do not loose hope. Try your luck again :)</div>"; 
      }else{ 
       // win 
         $wa = $amount*$percent; 
         $win_amount = $wa/100; 
         $final_cash = $win_amount+$balance; 

         // update database with winning amount 
         $stmt = $pdo->prepare("UPDATE mem_balance SET mb_acbal = :bal WHERE mb_user = :user"); 
         $stmt-> bindValue(':bal', $final_cash); 
         $stmt-> bindValue(':user', $sessionid); 
         $stmt-> execute(); 

       $msg = "<div class='message-success'>Congratulations buddy! You won... <strong>$".$win_amount."</strong> has been credited to your account.</div>"; 
      } 
      } 
     } 
     } 
     echo json_encode(array('status' => $msg, 'deduct' => $deduct)); 
} 
?> 
在上面的腳本

這裏,當if($wallet < $amount)條件爲假,執行else條件,腳本工作正常,並根據需要返回<div class='message-success'>。但是,如果if($wallet < $amount)條件爲真,那麼它不會返回<div class='message-error'>,並且加載映像將繼續移動(就像等待響應一樣),但不會收到任何響應。自從幾天以來,我被卡住了,但無法找到任何解決方案。請幫忙。

+0

考慮清理你的代碼。你至少有5個嵌套的if語句,這對於可讀性,可維護性以及與編碼有關的其他任何事情來說都是可怕的。看起來你可能在if($ coin ==''){'語句之前缺少一個大括號,但我無法確定。 – Augwa

回答

1

我不知道,但我認爲在您的PHP腳本$扣除已在else塊內聲明。如果

腳本執行,所以當和($錢包< $金額)條件計算結果爲真,那麼其他部分被跳過,直接返回此: -

return echo json_encode(array(
     'status' => $msg, 
     'deduct' => $deduct 
    )); 

所以它可能是沒有確認$扣除的情況。嘗試通過在if塊之前聲明$扣除來執行。

+0

你沒有正確地得到我的問題..再讀一遍..你只是說我的問題是相反的。當if($ wallet <$ amount'條件爲真時,它裏面的$ msg應該顯示,但是當這是真的json沒有返回msg ... –

+0

你已經在else部分中聲明瞭變量'$ deduct',所以當'if($ wallet <$ amount)'爲'true'時,'$ deduct'是一個''json_encode()''的未知變量,你應該在'if($ wallet <$ amount)'之前嘗試添加'$ deduct = 0'。 –

+0

現在你破解了它的夥計......但不是聲明'$ deduct = 0 '我剛剛在if語句之前轉移了'$ deduct = $ wallet- $ amount;'現在它的工作很完美..這讓我陷入了地獄2天......感謝好友...... –

相關問題