Tutorial :Integrating legacy Ajax script in CakePHP



Question:

I have a legacy script that I would like to integrate in a cakephp app. The script makes use of $_POST and such and since I'm quite a noob I would need some help for the integration.

Here is how the script looks like:

THE JAVASCRIPT:

  • prototype.js
  • builder.js (these two are from the prototype fw)
  • lib.js (makes a ajax requests to remote.php)

THE PHP

  • remote.php (contains FastJSON class and $_POST vars)

    if ($_POST['cmd'] == 'SAVETEAM' && $_POST['info'])  {      $INFO = json_decode(str_replace('\"', '"', $_POST['info']));  $nr = 1;    $SORT = array($INFO->GK, $INFO->DEF, $INFO->MID, $INFO->FOR, $INFO->RZ);    foreach ($SORT as $STD)  foreach ($STD as $v) mysql_query("UPDATE players_teams SET fieldposition = ".$nr++." WHERE player_id = {$v->player_id} AND team_id = {$v->team_id}") or die(mysql_error());    // CAPTAION  mysql_query("UPDATE `teams` SET captain = '{$_POST['captain']}' WHERE `user_id` = {$_POST['userid']}") or die(mysql_error());  

    }

  • transfers.php (containts the form that uses the javascript and link to the JS)

I have really no idea how to structure the files and calls in cakephp. Currently I have "Undefined index: cmd [APP/vendors/remote.php, line 230]" errors since I use $_POST['cmd'] (I placed remote.php in Vendors and included it, the JS was just included old fashion way, as a link and appears in the source code). How can I make this work? I'm sorry but I'm not familiar with AJAX and Cake... If you want a full look at the code, here it is: http://octavian.be/thecode.zip

Thank you for reading and helping me out.


Solution:1

  1. There's still PHP in CakePHP, $_POST and all the other things are still usable.
  2. The Javascript in the browser doesn't care about Cake at all, it just runs as usual.
    (Unless it retrieves data from the server, which you may have to emulate using Cake.)
  3. The legacy PHP script is quite horrible.
  4. Figure out what it does, document it.
  5. Look at all the variables (var_dump()) in various stages to see what kind of information the script extracts.
  6. Clean it up, fix any errors.
  7. Translate the database operations to Cake.
  8. Ask more specific questions as you come across them.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »