1 Replies - 2097 Views - Last Post: 22 September 2012 - 08:26 AM

#1 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Executing separate code blocks from jQuery AJAX calls

Posted 22 September 2012 - 03:24 AM

I have a simple webpage which runs a few tasks like when clicked upon a button it displays a set of results returned from a database and generate a unique code for each new record inserted from the page.

I'm using AJAX for these tasks. Now as you know you put the PHP page which handles those tasks the the AJAX function's url.

$.ajax(
        {
            type        : 'GET',
            url         : 'requestPage.php',

            //rest of the code
        }); 



requestPage.php looks like this

<?php

include_once 'DatabaseHandler.php';
$db = DatabaseHandler::getInstance();


//loading records
try
{
    //code for loading records

    echo json_encode($results);
}
catch (Exception $e)
{
    echo $e->getMessage();
}


//generating a new ID
try
{
     //ID generating code
}
catch (Exception $e)
{
    echo $e->getMessage();
}

?>




Is it possible to put all the code which handles different tasks into a single PHP page and when an AJAX call requests for a certain task, the page find the code which does the requested one and returns its results?

if loading_records Is called
execute the code for loading records
and return the results
else if new_id Is called
execute new ID generating code
and return the result


Thank you :)

Is This A Good Question/Topic? 0
  • +

Replies To: Executing separate code blocks from jQuery AJAX calls

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,418
  • Joined: 23-August 08

Re: Executing separate code blocks from jQuery AJAX calls

Posted 22 September 2012 - 08:26 AM

Yes, if you pass a parameter to the PHP page and execute different code paths based on the value in that parameter.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1