Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7159497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:14:28+00:00 2026-05-28T13:14:28+00:00

I have some ajax calls in multiple JavaScript functions. Each one does a post

  • 0

I have some ajax calls in multiple JavaScript functions. Each one does a post / get to a functions.php file

The functions.php has multiple functions that should correspond with the ones from JavaScript.

For example in js I have:

function one() {
$.ajax({ 
    type: 'POST', 
    url: 'http://www.example.com/functions.php', 
    data: vals,
    dataType:'json',
    success: function (data) {
        alert(data);
    }
});
}

function two() {
$.ajax({ 
    type: 'POST', 
    url: 'http://www.example.com/functions.php', 
    data: othervals,
    dataType:'json',
    success: function (data) {
        alert(data);
    }
});
}

function three() {
$.ajax({ 
    type: 'GET', 
    url: 'http://www.example.com/functions.php',  
    data: { get_param: 'user' },
    dataType:'json',
    success: function (data) { 
        alert(data);
    }
        
});
}

In PHP I have something like this:

if (isset($_POST['city'])) {  // this comes from the javascript vals json object
$city = $_POST['city'];
   function one() {
    // do something

    return true;
   }
}

function two() {
 // do something
 return true;
}


if (isset($_GET['get_param']) && $_GET['get_param'] == 'user'){
function three() {
 // do something
return true;
}
}

Maybe the PHP side is a bit confusing the way I write it, but in the end I want the function one to only deal with the corespondent function from the PHP file. Obviously they don’t need to have the same name.

The PHP functions can return true or false or 1 or 0, and that suppose to be the alerted data alert(data);.

If there is more confusion on what I want please let me know and I’ll clarify.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:14:29+00:00Added an answer on May 28, 2026 at 1:14 pm

    Have you checked out a REST library style. It looks like your doing basically that but a bit more confusing. When i say REST library i do not mean a purely RESTful library, but instead each function in your back-end is navigable via url.

    The way your doing it is fine (as long as the functions data does not depend on any other function data (as it could lead to some funny results)). Its just a lot easier to do more of a restful approach. Its fairly simple to set up a rest library.

    I just find that doing the whole $_POST[…] and then keep doing it is just cumbersome over time and becomes harder and harder to manage, because there is always some new twist on what is needed then you end up with 100’s of methods for taking care of calling back end functions.

    MyApi = {    
        /**
         * The API Version
         */
        API_VERSION: "0.5",
        SITE_URL: "http//myurl.com/",
        /**
         * The API URL
         */
        apiURL: function(tokens) {
            return MyApi.SITE_URL + MyApi.API_VERSION + "/api/" + MyApi.apiTokenizer(tokens);
        },
        /**
         * The tokenizer for the API.
         */
        apiTokenizer: function(tokens) {
            var str = '';
            $.each(tokens, function(key, value) {
                str += value + "/";
            })
            return str;
        }
    }
    

    Thats the javascript for producing new api links, then you could have something like

    function callBackend(tokens) {
        $.ajax({ 
            type: 'POST', 
            url: MyLibrary.apiURL(tokens),
            dataType:'json',
            success: function (data) {
                alert(data);
            }
        });
    }
    

    On your backend you would need an .htaccess file like so

    RewriteRule 0\.5/(.*) api/_MyApi.php?v=0\.5&state=$1 [QSA]
    

    Then you could write a back end switch statement that would take apart the state (delimiters would be “/”) that would navigate you to the end time library call.

    <?php
        $state = MyParser::Parse($_REQUEST["state"]);
        $output = array();
        switch ($state) {
            case "case1":
                //do stuff
                break;
        }
    
        echo json_encode($output);
    ?>
    

    That way output is always handled the same way.


    Just as a note. That was a very VERY simple and INCOMPLETE implementation, but i find that its a lot easier to maintain than a $_POST that goes to 1 of 100 different files that all have very similar output and all of that.

    Cheers! Happy coding

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some ajax code that executes on mouseclick. It calls a file called
I have coded some JavaScript to perform an ajax call in an asp.net application.
I have a website where an ajax call will get some Json data from
I have 3 ajax call in one function and checkAjaxCompletion which checks each ajax
I have some AJAX calls that render PartialViewResults via the jQuery.AJAX method. This works
I make some Ajax calls from inside a javascript object.: myObject.prototye = { ajax:
I have some ajax calls on the document of a site that display or
i'm wanted to perform some ajax calls in a certain way. I have a
I have some (potentially) long-running ajax calls that I would like to abort if
I'm getting some strange results sending multiple asynchronous AJAX calls to the same java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.