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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:36:44+00:00 2026-05-25T01:36:44+00:00

I have many functions on a page, where the following code is exactly the

  • 0

I have many functions on a page, where the following code is exactly the same, except for the data being sent, and the success div to display the result to. For example:

One ajax post:

$.ajax({
    type: 'POST',
    url: 'unique_file.php',
    data: {qty: 2, item_id: 13},
    cache: false,
    success: function(data) {
       $('#return_div').html(data);
    }
});  

Another ajax post:

$.ajax({
    type: 'POST',
    url: 'another_unique_file.php',
    data: {no_shipping: 4},
    cache: false,
    success: function(data) {
      $('#a_different_div').html(data);
    }
});

So I was thinking it would be ideal to make this into a function like so

function do_ajax(post_file,results_div,post_data) {
$.ajax({
    type: 'POST',
    url: post_file,
    data: {post_data}, // this doesn't appear to work
    cache: false,
    success: function(data) {
      $(results_div).html(data);
    }
});
}

Then I can achieve #1 ajax like so:

do_ajax('unique_file.php','#return_div','qty: 2, item_id: 13');

This seems logical to me, but the limits of my logic are how to get the data into my function. Perhaps the post_data is not just a string like that but more something like:

do_ajax('purchase_create.php','#create_purchase_window','item_id:'+$item_id+', qty: '+$qty+'');

Which looks pretty messy… but mostly, it just doesn’t work. Is there a cleaner way of perhaps putting all the data into an array and giving that to the function?

  • 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-25T01:36:46+00:00Added an answer on May 25, 2026 at 1:36 am

    You’re on the right track:

    function do_ajax(post_file,results_div,post_data) {
    $.ajax({
        type: 'POST',
        url: post_file,
        data: post_data,
        cache: false,
        success: function(data) {
          $(results_div).html(data);
        }
    });
    }
    
    do_ajax('purchase_create.php, '#create_purchase_window', {
        item_id: 3,
        qty: 2
    });
    

    You’re passing an object to POST, so just pass that object into your do_ajax function and pass it along.

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

Sidebar

Related Questions

I have written a HTML page which has following code. <div id=adsListContainer style=margin: 5px;>
I have the following code in the head tag of my page: <script type=text/javascript>
i have many events such as $('.hidefile').click(function (event) { On page load and on
I have many structs (classes) and standalone functions that I like to compile separately
I have a class which has many small functions. By small functions, I mean
I have used many of the Convert.To..... functions for conversion , but I didn't
Many people have argued about function size. They say that functions in general should
Not many are aware of this feature, but Python's functions (and methods) can have
Many of the standard c library (fwrite, memset, malloc) functions have direct equivalents in
I got a .ctp web page with the following lines of code: <script type=text/javascript

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.