0

I've done plenty of research and keep going round in circles. I have created an auction application which requires a "live bidding" process:

So I need to be able to perform GET statement to find the current highest bid(this will require some validation) I need to then perform a POST statement(again performing validation beforehand)

Difficulties:

I cannot find a solution that regularly updates the current bid. i.e. every 3-5 seconds check the bid is correct or just update it

There is a lot of conflicting information and as a newbie to PHP and Laravel I'm really stuck. And I do appreciate the amount of other similar questions out there but none adequately explain what is required or go into the level of detail required. so much so I'm starting to think jQuery/AJAX is not the correct action to complete this task.

Main.js (loaded in header of all php files )

var delay = 5000;

var getCurrentHighestBid = function() {
// perform validation here, if necessary
var url = '/items';   // insert your URL here

$.get(url, null, handleGetCurrentHighestBidResponse);
};

var handleGetCurrentHighestBidResponse = function(response) {
    // check for nulls in response here, handle exceptions, etc
    // then insert your bid data into the DOM, which may look
    // something like:
    $('.winner').html(response.Html);

    setTimeout(getCurrentHighestBid , delay);
};

itemController.php (snippet)

public function show($id)
    {
            $item = Item::find($id);
            //find higest bid fo item_auction id 
            $winningBid = Item::find($id)->bids()->max('bid_amount');
            //var_dump($item);
        return View::make('items.show', compact('item', 'winningBid'));




    }

show.blade.php (view) (snippet)

<h4 id="winner">{{$winningBid }}</h4>
kris
  • 90
  • 1
  • 1
  • 12
  • *** i have no code at all that is worth adding. so the interval difficulty is not the only difficulty – kris Mar 10 '15 at 22:14
  • 1
    kris - it's really hard to help somebody that just has a description of what they want without code. Please add your code - even if you don't think it's worth adding. If you really don't have anything, then write some pseudo code - anything so that we have something more than a vague verbal description of the problem... – Taryn East Mar 10 '15 at 22:24

1 Answers1

1

This is an ideal candidate for javascript/jQuery.

EDIT: I've updated the code to be simpler and more robust. The GET function is called (on a delay) from the response, this handles the delay from the server as well.

So basically:

  1. Call the server
  2. Wait for the response
  3. Process the response
  4. Wait (set by delay time)
  5. Repeat step 1

The GET could look something like this (requires jQuery):

var delay = 5000;

var getCurrentHighestBid = function() {
    // perform validation here, if necessary
    var url = '';   // insert your URL here

    $.get(url, null, handleGetCurrentHighestBidResponse);
};

var handleGetCurrentHighestBidResponse = function(response) {
    // check for nulls in response here, handle exceptions, etc
    // then insert your bid data into the DOM, which may look
    // something like:
    $('.bid-details').html(response.Html);

    setTimeout(getCurrentHighestBid, delay);
};
BrendanJefferis
  • 139
  • 1
  • 6
  • 1
    Why not just use `setInterval()`? – lukasgeiter Mar 10 '15 at 22:38
  • 1
    Great question, read more here http://stackoverflow.com/questions/729921/settimeout-or-setinterval – BrendanJefferis Mar 10 '15 at 22:39
  • var url = '/items/{id}'; i have added this as my url as it appears in my routes file. is this correct? as currently the value does not update. i have added all your code to my Main.js file – kris Mar 10 '15 at 23:04
  • $('.bid-details').html(response.Html); i have changed this to winner instead of bid-detials as this is the id of my

    – kris Mar 10 '15 at 23:06
  • @kris no that URL from the routes file is not an actual URL, it is like a template to tell your application what sort of URLs to expect. The jQuery GET method requires an actual URL, e.g., '/items/123456' the URL here would point to a server method (presumably called something like 'GetCurrentHighestBid' which then calls your database, etc and returns it to the client. – BrendanJefferis Mar 10 '15 at 23:17
  • @BrendanJeffries if my pages must include a unique id in url how can i circumvent this issue? or am i asking stupid questions here? my grateful thanks for all your help – kris Mar 10 '15 at 23:19
  • @kris also note that in the above example, I am assuming a response object to have a property called 'Html' which would have some HTML to put into the DOM. This will no doubt need to be changed based on your application, but the general idea is just: 'this is the part where you insert the data retrieved from the server into your page, using jQuery'. – BrendanJefferis Mar 10 '15 at 23:21