简体   繁体   English

将客户端数据传递到服务器端方法

[英]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.

相关问题 将数据PHP(客户端)传递到C#(服务器端) - Passing data PHP(client-side) to C#(server-side) 在服务器端获取远程数据并在服务器上进行处理是否比将数据传递给客户端进行处理更快? - Is fetching remote data server-side and processing it on server faster than passing data to client to handle? 处理数据服务器端与客户端 - Processing data server-side vs client-side dataTable:将额外的数据传递到php服务器端脚本 - dataTable: passing extra data to php server-side script 客户端或服务器端处理? - Client-side or server-side processing? 访问服务器端数据 - Accessing server-side data 数据表从服务器端处理在客户端搜索数据 - Datatables search data on client-side from a server-side processing 如何在发送到服务器端或客户端之前加密我网站上的数据? - How to encrypt data on my website before sending to server-side or client side? 从JSON数据(客户端或服务器端)获取JSONPath - Get JSONPath from JSON data (client-side or server-side) 将数据从服务器端数据表传递到模态 | 数据表的自定义行按钮 - Passing of data from Server-Side dataTable to a modal | Custom Row Button for dataTable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM