我正在尝试处理查询并成功加载jSON数据。在这里,问题在于jSON数据已成功返回并显示,但从第二时间开始。是的,你看的没错。我的意思是,当我按下“提交”按钮并且通过AJAX第一次处理数据时,jSON数据返回未更新。但是,当我第二次或第三次或第四次按下提交按钮时,将显示更新的数据。为什么它第一次不起作用?在这里,我正在尝试更新电子钱包余额和表格数据。
HTML
<div class="col-md-6">
<div class="card card-danger">
<div class="card-header">
<h3 class="card-title">Request Pin</h3>
</div>
<form role="form" method="post" action="" name="requestPin">
<div class="card-body">
<div class="form-group">
<label>Enter Amount (₹)</label>
<input type="text" class="form-control" placeholder="1500" name="amount">
</div>
</div>
<div class="card-footer">
<div class="row">
<div class="col-md-4">
<input type="submit" class="btn btn-primary" id="requestPin" value="Request">
</div>
<div class="col-md-8 text-right">
Wallet Balance: ₹<span id="wallet"><?php echo $bal['wal_balance']; ?></span>
</div>
</div>
</div>
</form>
<div class="overlay">
<i class="fa fa-refresh fa-spin"></i>
</div>
</div>
</div>
<div class="col-md-6">
<div class="message"></div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Request History</h3>
</div>
<div class="card-body table-responsive p-0">
<table class="table table-hover" id="pinReqHist">
<tr>
<th>ID</th>
<th>Amount</th>
<th>Request Date</th>
<th>Status</th>
</tr>
<?php
while($pin = $pinReq->fetch()){ extract($pin);
if($pr_status == 'approved'){
$statusColor = "text-success";
}else if($pr_status == 'denied'){
$statusColor = "text-danger";
}else{
$statusColor = "text-warning";
}
?>
<tr>
<td><?php echo $pr_id; ?></td>
<td>₹<?php echo $pr_amount; ?></td>
<td><?php echo date('jS F, Y (h:i a)', strtotime($pr_date)); ?></td>
<td><span class="<?php echo $statusColor; ?>"><?php echo ucfirst($pr_status); ?></span></td>
</tr>
<?php } ?>
</table>
</div>
</div>
</div>
request-pin.php
<?php
session_start();
include('../config/db.php'); include('../functions.php');
$msg = null;
$userid = (!empty($_SESSION['loggedin']))?$_SESSION['loggedin']:null;
$amount = (!empty($_POST['amount']))?$_POST['amount']:null;
if($_POST){
$balance = $pdo->prepare("SELECT wal_balance FROM wallet WHERE wal_user = :user");
$balance-> bindValue(':user', $userid);
$balance-> execute();
$bal = $balance->fetch();
$product = $pdo->prepare("SELECT pro_price FROM products
LEFT JOIN user_products ON products.pro_id = user_products.up_product
WHERE up_user = :user");
$product-> bindValue(':user', $userid);
$product-> execute();
$pro = $product->fetch();
if($amount == ''){
$msg = "Amount is required.";
echo json_encode(array('status' => alert_danger_dismiss($msg), 'bal' => number_format($bal['wal_balance'],2,'.',',')));
}else if(!ctype_digit($amount)){
$msg = "Please enter only numbers.";
echo json_encode(array('status' => alert_danger_dismiss($msg), 'bal' => number_format($bal['wal_balance'],2,'.',',')));
}else if($amount%$pro['pro_price'] != 0){
$msg = "Please enter amount in multiples of your product price. For eg., if your product price is ₹500 then you can enter either ₹500 or ₹1000 or ₹1500 and so on.";
echo json_encode(array('status' => alert_danger_dismiss($msg), 'bal' => number_format($bal['wal_balance'],2,'.',',')));
}else{
if($bal['wal_balance'] < $amount){
$msg = "Insufficient balance.";
echo json_encode(array('status' => alert_danger_dismiss($msg), 'bal' => number_format($bal['wal_balance'],2,'.',',')));
}else{
$insert = $pdo->prepare("INSERT INTO pin_request(pr_user, pr_amount)VALUES(:user, :amt)");
$insert-> bindValue(':user', $userid);
$insert-> bindValue(':amt', $amount);
$insert-> execute();
if($insert){
$update = $pdo->prepare("UPDATE wallet SET wal_balance = wal_balance-".$amount." WHERE wal_user = :user");
$update-> bindValue(':user', $userid);
$update-> execute();
$wallet = $pdo->prepare("SELECT wal_balance FROM wallet WHERE wal_user = :user");
$wallet-> bindValue(':user', $userid);
$wallet-> execute();
$wal = $wallet->fetch();
$pinReq = $pdo->prepare("SELECT * FROM pin_request WHERE pr_user = :user ORDER BY pr_id DESC");
$pinReq-> bindValue(':user', $userid);
$pinReq-> execute();
$table = "<table class='table table-striped' id='pinReqHist'>
<tr>
<th>ID</th>
<th>Amount</th>
<th>Request Date</th>
<th>Status</th>
</tr>";
while($pin = $pinReq->fetch()){ extract($pin);
if($pr_status == 'approved'){
$statusColor = "text-success";
}else if($pr_status == 'denied'){
$statusColor = "text-danger";
}else{
$statusColor = "text-warning";
}
$table .= "<tr>
<td>".$pr_id."</td>
<td>₹".$pr_amount."</td>
<td>".date("jS F, Y (h:i a)", strtotime($pr_date))."</td>
<td><span class='".$statusColor."'>".ucfirst($pr_status)."</span></td>
</tr>";
}
$table .= "</table>";
$msg = "Pin request sent!";
echo json_encode(array('status' => alert_success_dismiss($msg), 'bal' => number_format($wal['wal_balance'],2,'.',','),
'table' => $table));
}else{
$msg = "Server Error! Please try again.";
echo json_encode(array('status' => alert_danger_dismiss($msg), 'bal' => number_format($bal['wal_balance'],2,'.',',')));
}
}
}
}
?>
AJAX
$(document).ready(function(){
// Pin Request
$("#requestPin").click(function() {
var form = document.requestPin;
var dataString = $(form).serialize();
$.ajax({
type: "POST",
dataType : "json",
url: "processes/request-pin.php",
data: dataString,
cache: true,
beforeSend: function(){
$('.message').hide();
$('.overlay').fadeIn();
},
success: function(json){
if($('.message').find('#responseBox').hasClass('alert-success')){
setTimeout(function(){
$('#wallet').html(json.bal);
$('#pinReqHist').html(json.table).fadeIn();
}, 2000);
}
setTimeout(function(){
$('.overlay').fadeOut();
$('.message').html(json.status).fadeIn();
}, 2000);
}
});
return false;
});
});
答案 0 :(得分:0)
我找到了解决方案。这是由于var list = theEnumerable.ToList() // iterate once
list.Last() == list.Last() // I don't see how this makes sense, but I guess you have your own logic
函数由于$('.message')
功能而延迟。
我必须将setTimeOut()
从$('.message')
函数中取出,并在外部初始化jSON消息。之后,我像往常一样在动画效果的setTimeOut()
函数中显示了它。
我必须从此更改成功功能
setTimeOut()
对此
success: function(json){
if($('.message').find('#responseBox').hasClass('alert-success')){
setTimeout(function(){
$('#wallet').html(json.bal);
$('#pinReqHist').html(json.table).fadeIn();
}, 2000);
}
setTimeout(function(){
$('.overlay').fadeOut();
$('.message').html(json.status).fadeIn();
}, 2000);
}