gpt4 book ai didi

javascript - 通过php在用户端显示服务器错误

转载 作者:行者123 更新时间:2023-12-01 01:54:02 25 4
gpt4 key购买 nike

我有一个用户最终页面,即带有按钮的index.php

<button class="rightbtn" type="button" id="submitamt" style="display:none; ">Submit</button>

我的ajax代码:
<script>
function myFunction() {
alert("You need to login before negotiating! However you can purchase the product without negotiating");
}
var startClock;
var submitamt;
var walkaway;
var digits;

$(function() {
startClock = $('#startClock').on('click', onStart);
submitamt = $('#submitamt').on('click', onSubmit);
walkaway = $('#walkaway').on('click', onWalkAway);
digits = $('#count span');
beforeStart();
});

var onStart = function(e) {
startClock.fadeOut(function() {
startTimer();
submitamt.fadeIn(function() {
submitamt.trigger('click'); // fire click event on submit
});
walkaway.fadeIn();
});
};

var onSubmit = function(e) {
var txtbox = $('#txt').val();
var hiddenTxt = $('#hidden').val();
$.ajax({
type: 'post',
url: 'test2.php',
dataType: 'json',
data: {
txt: txtbox,
hidden: hiddenTxt
},
cache: false,
success: function(returndata) {

console.log(returndata);

},
error: function() {
console.error('Failed to process ajax !');
}
});
};

var onWalkAway = function(e) {
//console.log('onWalkAway ...');
};

var counter;
var timer;
var startTimer = function() {
counter = 120;
timer = null;
timer = setInterval(ticker, 1000);
};

var beforeStart = function() {
digits.eq(0).text('2');
digits.eq(2).text('0');
digits.eq(3).text('0');
};

var ticker = function() {
counter--;
var t = (counter / 60) | 0; // it is round off
digits.eq(0).text(t);
t = ((counter % 60) / 10) | 0;
digits.eq(2).text(t);
t = (counter % 60) % 10;
digits.eq(3).text(t);
if (!counter) {
clearInterval(timer);
alert('Time out !');
resetView();
}
};

var resetView = function() {
walkaway.fadeOut();
submitamt.fadeOut(function() {
beforeStart();
startClock.fadeIn();
});
};
</script>

单击此按钮后,通过ajax调用后端页面(page.php)。当此后端脚本运行时,我希望添加检查条件;如果未完全填充,我想在前端显示错误。 page.php上的代码是
<?php
session_start();
require 'connection.php';
$sql01 = "SELECT * FROM usertable where email='".$_SESSION['email']."' ";
$result01 = mysqli_query($con, $sql01);
if (mysqli_num_rows($result01) > 0)
{
while($row = mysqli_fetch_assoc($result01))
{
$userid = $row["id"];
}
}

$presentdate = date("Y/m/d");

$sql00 = "SELECT * FROM userrecord where userid='".$userid."' and datee='".$presentdate."'";
$result00 = mysqli_query($con, $sql00);
if (mysqli_num_rows($result00) > 0)
{
while($row = mysqli_fetch_assoc($result00))
{
$recordcounter = $row["recordcounter"];
}
}

if($recordcounter>=3)
{
echo "limit reached"; // wish to display this message on index.php page
}
else
{
/******run the code********/
}
?>

任何人都可以告诉我如何在单击“提交”按钮时在index.php页面上将 limit reached消息显示为警报框

最佳答案

建议:如果PHP错误,则为return "{error: true, message: 'limit reached'}"

AJAX调用完成后,用变量response捕获响应并添加检查。

dataType: 'json'属性添加到您的AJAX对象。 (将响应解析为Javascript对象)。

将您的成功功能更改为此:

function(response) {
if(response.error){
alert(response.message);
}
}

这使您的代码可读性强,仅查看客户端的人就可以了解正在发生的事情。

关于javascript - 通过php在用户端显示服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30979974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com