Php 使用Ajax从数据库获取结果
如果在输入valueonblur或onkeyup事件时未满足特定条件,我希望显示信息并禁用流程按钮。我试过很多例子,但没有一个给我结果。有人能帮我吗 Ajax代码:Php 使用Ajax从数据库获取结果,php,jquery,ajax,Php,Jquery,Ajax,如果在输入valueonblur或onkeyup事件时未满足特定条件,我希望显示信息并禁用流程按钮。我试过很多例子,但没有一个给我结果。有人能帮我吗 Ajax代码: <script type="text/javascript" src="includes/scripts/newJquery.js"></script> <script type="text/javascript"> $(document).ready(function () {
<script type="text/javascript" src="includes/scripts/newJquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("select.custid").change(function () {
var selectedCustomer = $("#amt").val();
var selectedCustId = $("#custid").val();
$.ajax({
type: "POST",
url: "process-loan.php",
data: {
custQual: selectedCustomer,
custid: selectedCustId
}
}).done(function (data) {
$("#qualify").html(data);
});
});
});
</script>
下面是php页面
<th>Customer No:</th>
<td>
<select name="custid" class="custid" id="custid">
<option>Select Customer No</option>
<?php while ($rw = mysqli_fetch_array($get)) { ?>
<option value="<?php echo $rw['custid'] ?>"><?php echo $rw['custid'] ?></option>
<?php } ?>
</select>
</td>
<tr>
<th>Requesting Amount:</th>
<td><input type="number" name="amount" value="0.00" id="amt"/></td>
</tr>
<tr>
<td id="qualify"> </td>
<td id="qualify"> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="save" value="Process Loan" class="btn btn-success" id="pButton"/>
将响应ajax调用的process-loan.php脚本:
<?php
if (isset($_POST["amt"])) {
include 'includes/session.php';
include 'includes/db_connection.php';
$amt = $_GET["amt"];
$custid = $_POST["custid"];
// Query the Databased based on the amount and the UserId
if ($amt !== NULL) {
$gets = "SELECT * FROM tab_customer_dailycontribution WHERE custid='" . $custid . "' AND transactionDate BETWEEN '2015-09-01' AND '2015-09-30'";
$get = mysqli_query($connection, $gets);
$sum = 0.00;
while ($row = mysqli_fetch_array($get)) {
$sum += $row['amountContribute'];
}
if ($sum >= $amt) {
//qualify for loan $ enable the Process Button to save
echo "You are Qualify to Apply";
} else {
//disqualify for loan $ disable the process button until condition is meant.
echo "Insufficient Fund: Unqualify to Apply";
}
//end if condition
}
}
?>
这是为了演示,所以我评论了与mysql相关的事情:首先在process-loan.php中更改密钥 您的查看页面: process-loan.php
什么东西不起作用?你被困在哪里了?你有$amt=$\u得到[amt];和if$amt!==空,但您的金额不是从POST提交的吗?您没有从ajax发布任何参数amt!!在代码中混合GET和POST方法金额的关键是:custQualI犯了一个错误,它应该是$\u POST而不是$\u GET。当我从“金额”字段中松开焦点时,我想回显“符合条件”或“不符合条件”。加载页面时,“处理”按钮将显示而不是隐藏。此外,如果您没有在“金额”字段中输入任何值,并且在“处理”按钮上单击了tab键,onblur始终显示Qualife to apply。否,它将被隐藏。请参阅document ready函数中的第一行。消息的出现是因为sum中的静态值,您可以更改它并使用数据库值。将其从static更改回从DB获取后,在下一个选项卡上仍然显示Qualife to apply。即使对于zero0也是如此。可能有什么问题?那你为什么要将customerNo从DB获取改为数字迭代?我没有你的数据库管理员。我只能用静态值进行演示。你的条件是如果$sum>=$amt,那么检查$sum和$amt中的值。你必须决定功能。什么是零金额或总和。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#pButton").hide();
$("#amt").on("blur",function(){
var selectedCustomer = $("#amt").val();
var selectedCustId = $("#custid").val();
$.ajax({
type: "POST",
url:"process-loan.php",
data:{custQual:selectedCustomer,custid:selectedCustId},
success:function(data){
var data=JSON.parse(data);
$("#qualify").html(data.msg);//your message
if(data.status == 0){
$("#pButton").show();//showing button if qualified
}else{
$("#pButton").hide();
}
}
});
});
});
</script>
<th>Customer No:</th>
<td><select name="custid" class="custid" id="custid">
<option>Select Customer No</option>
<?php $i =0; while($i <4){?>
<option value="<?php echo $i?>"><?php echo $i?></option>
<?php $i++;}?></select></td>
<tr>
<th>Requesting Amount:</th>
<td><input type="number" name="amount" value="0.00" id="amt"/></td>
</tr>
<tr>
<td ><div id="qualify"></div></td><!-- added div in td to show message it can be outside of your table !-->
<td> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="save" value="Process Loan" class="btn btn-success" id="pButton"/>
<?php
if(isset($_POST["custQual"])){
// include 'includes/session.php';
// include 'includes/db_connection.php';
$amt = $_POST["custQual"];//here use $_POST not $_GET
$custid = $_POST["custid"];
// Query the Databased based on the amount and the UserId
if($amt !== NULL){
$sum=100000;//static value for demo
if($sum >= $amt){
//qualify for loan $ enable the Process Button to save
$res["status"]=0;
$res["msg"]="You are Qualify to Apply";
}else{
//disqualify for loan $ disable the process button until condition is meant.
$res["status"]=1;
$res["msg"]= "Insufficient Fund: Unqualify to Apply";
}//end if condition
}else{
$res["status"]=1;
$res["msg"]= "put some amount first";
}
echo json_encode($res);
}