我有一个页面,显示用户是否已付款。如果他们没有付款,我需要向他们展示一个大红色X
的图像并显示一个付款按钮,如果他们已付款显示一个大的绿色支票。我知道如何为php脚本执行代码,让我们调用它pay.php
,但我需要弄清楚主页的ajax的逻辑。所以我在想这样的事情:
<div id="payment">
<div id="image"></div>
<div id="pay_text></div>
</div>
<script>
$(document).ready(function(e) {
if(<?php echo $is_logged_in; ?>) {
$('#payment').load(function() {
$.ajax({
data: $this.serialize(), // get the form data
type: "POST", // GET or POST
url: "Scripts/pay.php", // the file to call
complete: function(data) {
if(data=="Yes") {
$('#image') //do some jquery to make the correct image display here
$('#pay_text').text("Yah you paid");
} else {
$('#image') //do some jquery to make the correct image display here
$('#pay_text').text("Sorry you didn't pay");
}
}
});
}
}
});
</script>
基本上,脚本会回显yes
或no
。我只想在用户登录后评估此负载(我需要查看该特定用户是否付费)。那么我的逻辑是写还是我离开了?
答案 0 :(得分:2)
你可能不应该使用load
那种方式,而且根本不需要它。
在ajax函数运行之前,您的PHP变量$is_logged_in
当然必须是true
,并且返回true
或false
通常是使用代码的方式,不返回yes
或no
,如果data
是true
或false
,您可以将其粘贴在if语句中,不要模糊!
对于使用ajax函数发送表单数据,有什么形式?什么是$this
?
你必须弄清楚用户是否在服务器端付费,而你真的不需要客户端的任何数据来做这件事,你需要做的就是检查你的数据库或者你是什么'用于查看该用户是否付费,并返回true
或false
。
<script type="text/javascript">
$(document).ready(function() {
if(<?php echo $is_logged_in; ?>) {
$.ajax({
type: "POST",
url: "scripts/pay.php"
}).done(function(data) {
if (data) {
$('#image').css('background', 'url(/yes.png)');
$('#pay_text').text("Yah you paid");
}else{
$('#image').css('background', 'url(/no.png)');
$('#pay_text').text("Sorry you didn't pay");
}
});
}
});
</script>