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 6600921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:41:11+00:00 2026-05-25T18:41:11+00:00

I want a JavaScript function that posts to a php, and returns the value,

  • 0

I want a JavaScript function that posts to a php, and returns the value, so that calling the function from another function in JavaScript gives me the value. To illustrate better please look at my current code:

function sumValues(number1, number2) {
    var return_val;
    $.post("sum_values.php", {number1: number1, number2: number2},
    function(result) {
        return_val = result;
    });

    return return_val;
}

However, the above is returning undefined. If I put alert(result) inside the function part in $.post, I can see the result. However if I call the function from another JavaScript file the return value return_val is ‘undefined’. How can I put the result from $.post inside return_val so that the function can return it?

  • 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-25T18:41:11+00:00Added an answer on May 25, 2026 at 6:41 pm

    AJAX is asynchronous meaning that by the time your function returns results might not have yet arrived. You can only use the results in the success callback. It doesn’t make sense to have a javascript performing an AJAX request to return values based on the results of this AJAX call.

    If you need to exploit the results simply invoke some other function in the success callback:

    function sumValues(number1, number2) {
        $.post("sum_values.php", { number1: number1, number2: number2 },
        function(result) {
            callSomeOtherFunction(result);
        });
    }
    

    and inside the callSomeOtherFunction you could show the results somewhere in the DOM.

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

Sidebar

Related Questions

I want to call a JavaScript function through PHP and store the returned value
Is there any javascript function that can encrypt data: For example i want to
I want to call a c# function from my javascript function. I have a
Summary I want to be able to call a JavaScript function from a Flex
I want to define a JavaScript class, Foo. Foo = function(value){ this.value = value;
basically i want to hold a parameter that retrieve value from $.post() call like
I want to write a javascript function, that will make an ajax request to
I have a javascript function that gathers two arrays, imagepaths and captions. I want
I want to make a link call a Javascript function through the onclick event
I want to run a simple JavaScript function on a click without any redirection.

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.