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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:04:15+00:00 2026-05-23T15:04:15+00:00

Hi this is an example of the code i want to run: $(‘#search1’).submit(function(){ var

  • 0

Hi this is an example of the code i want to run:

        $('#search1').submit(function(){
    var date = $('#date').val();
    var location = $('#location').val();
    var datastring = 'date=' + date + '&location=' + location;
    $.ajax({
        type: "POST",
        cache: "true",
        url: "search.php",
        dataType:"json",
        data: datastring,
        success: function(data){
            $('#main').html('')
            for ($i = 0, $j = data.bus.length; $i < $j; $i++) {

                //Create an object for each successful query result that holds information such as departure time, location, seats open...

                   $('#main').append(html);

            }

How would I go about coding the success function? I want the object to store each bus’ information so that the info can be displayed in the search result as well as being able to be referenced when the user confirms his RSVP later on. Thanks ahead of time

  • 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-23T15:04:16+00:00Added an answer on May 23, 2026 at 3:04 pm

    You can declare an object to use as a map in the containing scope:

    var busInfo = {};
    

    …and then if the bus entries have some form of unique identifier, you can record them like this:

    success: function(data){
        var $i, $j, bus;
    
        $('#main').html('')
        for ($i = 0, $j = data.bus.length; $i < $j; $i++) {
            // Remember this bus by ID
            bus = data.bus[$i];
            busInfo[bus.id] = bus;
    
            $('#main').append(html);
        }
    }
    

    And then later, when the user chooses a bus, use the chosen ID to get the full bus information:

    var bus = busInfo[theChosenId];
    

    This works because all JavaScript objects are key/value maps. Keys are always strings, but the interpreter will happily make strings out of what you give it (e.g., busInfo[42] = ... will work, 42 will become "42" implicitly).

    If you just want an array, your data.bus already is one, right?

    var busInfo = [];
    
    // ....
    
    success: function(data){
        var $i, $j;
    
        // Remember it
        busInfo = data.bus;
    
        $('#main').html('')
        for ($i = 0, $j = data.bus.length; $i < $j; $i++) {
    
            $('#main').append(html);
        }
    }
    

    (Note that JavaScript arrays aren’t really arrays, they too are name/value maps.)


    Update: I dashed off a quick example of the keyed object (live copy):

    HTML:

    <input type='button' id='btnLoad' value='Load Buses'>
    <br>...and then click a bus below:
    <ul id="busList"></ul>
    ...to see details here:
    <table style="border: 1px solid #aaa;">
      <tbody>
        <tr>
          <th>ID:</th>
          <td id="busId">--</td>
        </tr>
        <tr>
          <th>Name:</th>
          <td id="busName">--</td>
        </tr>
        <tr>
          <th>Route:</th>
          <td id="busRoute">--</td>
        </tr>
      </tbody>
    </table>
    

    JavaScript with jQuery:

    jQuery(function($) {
      // Our bus information -- note that it's within a function,
      // not at global scope. Global scope is *way* too crowded.
      var busInfo = {};
    
      // Load the buses on click
      $("#btnLoad").click(function() {
        $.ajax({
          url: "http://jsbin.com/ulawem",
          dataType: "json",
          success: function(data) {
            var busList = $("#busList");
    
            // Clear old bus info
            busInfo = {};
    
            // Show and remember the buses
            if (!data.buses) {
              display("Invalid bus information received");
            }
            else {
              $.each(data.buses, function(index, bus) {
                // Remember this bus
                busInfo[bus.id] = bus;
    
                // Show it
                $("<li class='businfo'>")
                  .text(bus.name)
                  .attr("data-id", bus.id)
                  .appendTo(busList);
              });
            }
          },
          error: function() {
            display("Error loading bus information");
          }
        });
      });
    
      // When the user clicks a bus in the list, show its deatils
      $("#busList").delegate(".businfo", "click", function() {
        var id = $(this).attr("data-id"),
            bus = id ? busInfo[id] : null;
        if (id) {
          if (bus) {
            $("#busId").text(bus.id);
            $("#busName").text(bus.name);
            $("#busRoute").text(bus.route);
          }
          else {
            $("#busId, #busName, #busRoute").text("--");
          }
        }
      });
    
    });
    

    Data:

    {"buses": [
        {"id": 42, "name": "Number 42", "route": "Highgate to Wycombe"},
        {"id": 67, "name": "Old Coach Express", "route": "There and Back"}
    ]}
    

    Off-topic: Note that I’ve added var $i, $j; to your success function. Without it, you’re falling prey to The Horror of Implicit Globals, which you can tell from the name is a Bad Thing(tm).

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

Sidebar

Related Questions

for example this code var html = <p>This text is <a href=#> good</a></p>; var
This is an example code from the prototype site. var url = '/proxy?url=' +
I want to try out the example code from this article: Load Recovery.gov Grant
As some example code, I might have something like this: $('a.parent').click(function(){ $('a.parent').each(function(){ $(this).stop(true,false).animate({ width:
Take this example code (ignore it being horribly inefficient for the moment) let listToString
In this example code, I'm trying to offset the Grid 's Canvas position by
There is this example code, but then it starts talking about millisecond / nanosecond
take a look at this example code: public class Comment { private Comment() {
The questions says everything, take this example code: <ul id=css-id> <li> <something:CustomControl ID=SomeThingElse runat=server
With regards this example from Code Complete: Comparison Compare(int value1, int value2) { if

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.