[英]Passing client data to server-side methods
I have a JavaScript file that checks for bids using json data. 我有一个JavaScript文件,该文件使用json数据检查出价。
Now the problem I have is the plugin for the countdown has an onExpiry function that won't let you pass parameters, when u do the callback is called before the counter expires. 现在,我的问题是倒计时插件具有onExpiry函数,当您在计数器到期之前调用回调函数时,该函数将不允许您传递参数。 On expiry of the countdown, it should set auction to Won, alert the user and add them to winners table. 倒计时到期时,应将“拍卖”设置为“赢得”,提醒用户并将其添加到“获奖者”表中。 Most of this is handled by php, but the function I have made requires the auction_id and the username to be passed, but without passing parameters I cant think of how to do this. 其中大多数是由php处理的,但是我已经完成的功能需要传递Auction_id和用户名,但是没有传递参数,我想不起来该怎么做。 Here is the code I have at present. 这是我目前拥有的代码。
Is Won PHP Function: 赢得了PHP函数:
function isWon($user, $auction){
$this->load->model('Auction_model', 'Auction');
$this->load->model('Bid_model', 'Bid');
$this->load->model('Customer_model', 'Customer');
$makeWon = $this->Auction->isWon($auction);
$customer = $this->Customer->getByUsername($user);
if ($makeWon){
$json = array(
'success' => 'Congratulations '.$user.' You have won.',
);
}
echo json_encode($json);
}
Common.Js 普通
function checkBids(){
var id = $('div.auction').attr('id');
$.ajax({
url: 'http://localhost/tinybid/index.php/auction/auctiondata',
dataType: 'json',
success: function(json) {
for (var i = 0; i < json.length; i++) {
$('#auction'+json[i]['i']+' li.user').html(json[i]['u']);
$('#auction'+json[i]['i']+' li.curPrice').html(json[i]['p']);
if (typeof json[i]['t'] =='number'){
$('#auction'+json[i]['i']+' li.timer').countdown({until: startTimer(json[i]['i'],json[i]['t']), onExpiry: isWon});
}
}
}
});
}
function startTimer(auction, stamp){
var time= new Date();
time.setTime(stamp*1000);
$('#auction'+auction+' li.timer').countdown('change', {until: time});
}
function isWon(user, auction){
if (user){
$.ajax({
url: 'http://localhost/tinybid/index.php/auction/isWon/'+user+'/'+auction,
dataType: 'json',
success: function(json) {
if (json['success']){
alert(json['success'])
}
}
});
}
}
Here is the Json returned on the checkBids() function 这是在checkBids()函数上返回的Json
[{"i":"5","t":1324374080,"p":"£ 0.13","u":"chrissy"}]
Is this the best way to create this countdown, or is the a better, cleaner way? 这是创建此倒数的最佳方法,还是更好,更清洁的方法? If this is the best way how can I get the functions to all work lol :) 如果这是最好的方法,我如何才能使所有工作的功能大声笑:)
You don't need the "onExpiry" to let you pass parameters. 您不需要“ onExpiry”来传递参数。 First - you can alway to the function 首先-您可以随时使用该功能
onExpiry : function(){ isWon(user, auction); }
Thus effectively passing the arguments in the annonymous function implementation. 从而有效地在匿名函数实现中传递参数。 But what I usually do is simply add a hidden input fields 但是我通常要做的只是添加一个隐藏的输入字段
<input id="user_id" type="hidden"/>
<input id="auction_id" type="hidden"/>
and in the "isWon" function - instead of getting "user" and "auction" as parameters simply do the following 并在“ isWon”函数中-无需获取“ user”和“ auction”作为参数,只需执行以下操作
var user_id = $("#user_id").val();
var auction_id = $("#auction_id").val();
And then pass it in ajax to server, or directly pass it in ajax like so 然后将其以ajax的形式传递到服务器,或者像这样直接以ajax的形式传递
$.ajax({
url: 'http://localhost/tinybid/index.php/auction/isWon/'+user+'/'+auction,
dataType: 'json',
data:{user_id:$("#user_id").val(), auction_id:$("#auction_id").val()},
success: function(json) {
if (json['success']){
alert(json['success'])
}
}
By the way - please note that the "URL" is weird in your example. 顺便说一句-请注意,您的示例中的“ URL”很奇怪。 Should start with "/" and not contain "index.php" in the middle. 应该以“ /”开头,中间不包含“ index.php”。 If I could have your file hierarchy I can resolve this too. 如果可以使用您的文件层次结构,也可以解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.