2017-04-02 28 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天......感谢好友...... –

相关问题