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

  • SEARCH
  • Home
  • 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 941875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:11:03+00:00 2026-05-15T22:11:03+00:00

I have a small problem with jQuery $.ajax() function. I have a form where

  • 0

I have a small problem with jQuery $.ajax() function.

I have a form where every click on the radio button or selection from the dropdown menu creates a session variable with the selected value.

Now – I have one of the dropdown menus which have 4 options – the first one (with label None) has a value="" other have their ids.

What I want to happen is to None option (with blank value) to remove the session and other to create one, but only if session with this specific select name doesn’t already exist – as all other options have the same amount assigned to it – it’s just indicating which one was selected.

I’m not sure if that makes sense – but have a look at the code – perhaps this will make it clearer:

$("#add_ons select").change(function() {
        // get current price of the addons
        var order_price_addon = $(".order_price_addon").text();
        // get price of the clicked radio button from the rel attribute
        var add = $(this).children('option').attr('label');
        var name = $(this).attr('name');
        var val = $(this).val();
        
        
        if(val == "") {
            var price = parseInt(order_price_addon) - parseInt(add);
            removeSession(name);
        } else {
            if(isSession(name) == 0) {
                var price = parseInt(order_price_addon) + parseInt(add);
            }   
            setSession(name, val);              
        }
        
        $(".order_price_addon").html(price);    
        setSession('order_price_addon', price);         
        updateTotal();
});

so – first of all when the #add_ons select menu triggers "change" we get some values from a few elements for calculations.

we get the label attribute of the option from our select which stores the value to be added to the total, name of the select to create session with this name and value to later check which one was selected.

now – we check whether the val == "" (which would indicate that None option has been selected) and we deduct the amount from the total as well as remove the session with the select’s name.

After this is where the problem starts – else statement.

Else – we want to check whether the isSession() function with the name of our selector returns 0 or 1 – if it returns 0 then we add to the total the value stored in the label attribute, but if it returns 1 – that would suggest that session already exists – then we only change the value of this session by recreating it – but the amount isn’t added to it.

Now isSession function looks like this:

function isSession(selector) {
    $.ajax({
        type: "POST",
        url: '/order.html',
        data: ({ issession : 1, selector: selector }),
        dataType: "html",
        success: function(data) {
            return data;
        },
        error: function() {
            alert('Error occured');
        }
    });
}

Now – the problem is – that I don’t know whether using return will return the result from the function – as it doesn’t seem to work – however, if I put the "data" in the success: section into the alert() – it does seem to return the right value.

Does anyone know how to return the value from the function and then compare it in the next statement?


Thanks guys – I’ve tried it in the following way:

function isSession(selector) {
    $.ajax({
        type: "POST",
        url: '/order.html',
        data: ({ issession : 1, selector: selector }),
        dataType: "html",
        success: function(data) {
            updateResult(data);
        },
        error: function() {
            alert('Error occured');
        }
    });
}

then the updateResult() function:

function updateResult(data) {
    result = data;
}

result – is the global variable – which I’m then trying to read:

$("#add_ons select").change(function() {
        // get current price of the addons
        var order_price_addon = $(".order_price_addon").text();
        // get price of the clicked radio button from the rel attribute
        var add = $(this).children('option').attr('label');
        var name = $(this).attr('name');
        var val = $(this).val();
        
        
        if(val == "") {
            var price = parseInt(order_price_addon) - parseInt(add);
            removeSession(name);
        } else {
            isSession(name);
            if(result == 0) {
                var price = parseInt(order_price_addon) + parseInt(add);
            }   
            setSession(name, val);              
        }
        
        $(".order_price_addon").html(price);    
        setSession('order_price_addon', price);         
        updateTotal();
    });

but for some reason – it doesn’t work – any idea?

  • 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-15T22:11:03+00:00Added an answer on May 15, 2026 at 10:11 pm

    The trouble is that you can not return a value from an asynchronous call, like an AJAX request, and expect it to work.

    The reason is that the code waiting for the response has already executed by the time the response is received.

    The solution to this problem is to run the necessary code inside the success: callback. That way it is accessing the data only when it is available.

    function isSession(selector) {
        $.ajax({
            type: "POST",
            url: '/order.html',
            data: ({ issession : 1, selector: selector }),
            dataType: "html",
            success: function(data) {
                // Run the code here that needs
                //    to access the data returned
                return data;
            },
            error: function() {
                alert('Error occured');
            }
        });
    }
    

    Another possibility (which is effectively the same thing) is to call a function inside your success: callback that passes the data when it is available.

    function isSession(selector) {
        $.ajax({
            type: "POST",
            url: '/order.html',
            data: ({ issession : 1, selector: selector }),
            dataType: "html",
            success: function(data) {
                    // Call this function on success
                someFunction( data );
                return data;
            },
            error: function() {
                alert('Error occured');
            }
        });
    }
    
    function someFunction( data ) {
        // Do something with your data
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a small problem with a jquery click trigger. in my index.php i
I'm trying to learn JQuery - and I have a small problem with ajax.
I have a small ajax jquery script which returns some XML for me. Whilst
i'm using jquery Ui slider for a web-project, and i have a small problem,
I'm starting out on jquery and ran into a small problem where $.ajax(...) is
I have a big problem writing a small piece of code using JS/jQuery (don't
I have a small problem in my code. I have a function, which require
have small problem, and would very much appreciate help :) I should convert byte
I have small problem with Spring MVC. Basically what I'm trying to do is
I have a small problem and I've figured out where when and why it

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.