DGPhoebus,
1. Use jQuery. If you love CakePHP and write your own JS (not using
the cake helpers) then this is the way to go, trust me ; ).
2. Echoing some text in a cake action w/o rendering a view is done
using: $this->autoRender = false; echo 'My text'; inside your
controller action
3. You may want to use "magic" numbers representing statuses. Neither
you nor the poor fellow who may have to make some adjustments 2
years down the road will remember them.
4. Here are some snippets showing how I would implement your problem:
controller action (ShowSeatsController::reserve):
--------------------------------------------------------------------------------
function reserve($show_id = null, $seat_id = null) {
if ($this->RequestHandler->isPost()) {
return $this->set('success', $this->ShowSeat->reserve($show_id,
$seat_id));
}
die('XSRF attacks suck ...');
}
--------------------------------------------------------------------------------
model function (ShowSeat::reserve):
--------------------------------------------------------------------------------
function reserve($show_id = null, $seat_id = null) {
$this->Show->id = $show_id;
$this->Seat->id = $seat_id;
if (!$this->Show->exists() || !$this->Seat->exists()) {
return 'error';
}
$showSeat = compact('show_id', 'seat_id');
if ($this->hasAny($showSeat)) {
return 'taken';
}
$this->create($showSeat);
if (!$this->save()) {
return 'error'
}
return 'ok';
}
--------------------------------------------------------------------------------
view (reserve.ctp):
--------------------------------------------------------------------------------
echo $javascript->object(compact('success'));
--------------------------------------------------------------------------------
jquery javascript (reserve.js):
--------------------------------------------------------------------------------
function reserveSeat(show_id, seat_id, callback) {
$.post('/show_seats/reserve/'+show_id+'/'+seat_id, {}, callback,
'json');
}
$(function() {
$('table#seats td').click(function() {
var data = $(this).attr('id').split('-');
reserveSeat(data[0], data[1], function(response) {
if (response.success == 'ok') {
// ...
}
});
});
});
If you wonder why I got so detailed here is b/c I think your example is
very nice for showing some of the strength of CakePHP in an easy to
understand way, so I hope others will like the above snippets as well.
(However, all of this was written off the cuff and not tested ^^).
-- Felix
--------------------------
My Blog: http://www.thinkingphp.org
My Business: http://www.fg-webdesign.de
DGPhoebus wrote:
> Okay here is the situation, I am working on a "Ticketing" website that
> I have already created in the "old" method of making websites (not
> CakePHP). I am redoing it now using CakePHP and I love it. Here is
> my problem. On the old site, I built out the layout of the theatre
> using a table with each seat being represented by a td cell. Each td
> cell had an onclick event that called my own code that either
> "reserved" or "released" the selected seat by an AJAX call. My AJAX
> call would return a value, '1','2', or '0' denoting "success",
> "failure", or "arleady taken" to my javascript function. I would then
> move on based on that response:
>
> var response = oXmlHttp.responseText;
> switch (response.charAt(0)) {
> case '0':
> // Alert that seat was already taken
> break;
> case '1':
> // Success
> break;
> case '2':
> // Alert Error
> break;
> }
>
> I have re-written this code using Prototype and can make the call to
> Controller/reserve . BUT.... 1. I have to create a useless "view" for
> this controller action and 2. I can't return a value of either 1,2, or
> 0. If I echo any of those characters the response text contains the
> entire "view" listing with "headers already sent" error. If I try to
> return any of those values it returns the entire view.
>
> Is there a way to call a controller action without needing a view? Is
> there a way to return a value from the controller action like I need
> 1,2, or 0?
>
> There are many actions that need to happen based on which return value
> I get from my AJAX call so it has to happen this way. The AJAX helper
> only appears to follow the form of SAVE then UPDATE and that is not
> enough.
>
> There has to be a way to do this without doing it the old way of
> making a call to a single script sitting in the document root. I am
> still very new to Cake so if this could be solved by something like a
> Component or something I would greatly appreciate some direction.
> Redoing this site with Cake has been fantastic and I have rolled right
> along up to this point, but I have stalled out.
>
> Please help!
>
> Daniel
>
>
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---