What is the process for fetching the chosen outcome from a subsequent webpage using HTML?

How can I display selected cities on a different webpage after clicking a button? Currently, I can only get the results on the same page. For example, if a user selects NYC and Delhi, those cities should be displayed on another HTML page.

document.getElementById('btn').onclick = function() {
               var markedCheckbox = document.getElementsByName('pl');
               for (var checkbox of markedCheckbox) {
                    if (checkbox.checked)
                         document.body.append(checkbox.value + ' ');
               }
          }
<h2 style="color:green"> Get all marked checkboxes value </h2>
               <h4> Select the City, you know </h4>
               <tr>
                    <td> Berlin: <input type="checkbox" id="check1" name="pl" value="Berlin"> </td>
                    <td> NYC: <input type="checkbox" id="check2" name="pl" value="NYC"> </td>
               </tr>
               <tr>
                    <td> Delhi: <input type="checkbox" id="check3" name="pl" value="Delhi"> </td>
                    <td> London: <input type="checkbox" id="check4" name="pl" value="London"> </td>
               </tr>
               <tr>
                    <td> Paris: <input type="checkbox" id="check5" name="pl" value="Paris"> </td>
                    <td> Dhaka: <input type="checkbox" id="check6" name="pl" value="Dhaka"> </td> <br> <br>

                    <button id="btn">Submit</button> <br>
                    <h4 style="color:green" id="result"></h4>

                    <script type="text/javascript" src="12.js"></script>

Answer №1

When it comes to displaying your data on a different page, it's essential to have saved your data somewhere. You can make use of PHP sessions or the localStorage feature in JavaScript. If you opt for using localStorage, you'll need to make some modifications to your JavaScript code.

document.getElementById("btn").onclick = function () {
  var markedCheckbox = document.getElementsByName("pl");
  var cities = [];

  for (var checkbox of markedCheckbox) {
    if (checkbox.checked) {
      cities.push(checkbox.value);
    }
  }

  // Save data into storage
  localStorage.setItem("cities", JSON.stringify(cities));

  // Retrieve data from storage on other pages
  var showCities = JSON.parse(localStorage.getItem("cities"));

  showCities.forEach((city) => {
    document.body.append(city + " ");
  });
};

Now that your data is stored in localStorage, you can easily access it from any HTML page.

If you wish to retrieve localStorage data on other HTML pages,

var showCities = JSON.parse(localStorage.getItem("cities"));

showCities.forEach((city) => {
  document.body.append(city + " ");
});

Simply insert this code within the script tag of the respective HTML page.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I've been attempting to access the Marketo API, but unfortunately, I haven't had any luck

I've been attempting to make a Marketo API call, but I keep encountering this issue: net::ERR_NAME_NOT_RESOLVED Here is the code snippet I'm using for the function: var marketoAPIcallURL = 'https://182-EMG-811.mktorest.com/rest/v1/ ...

Attempting to incorporate a YouTube video into an HTML webpage

Every time I try to embed a Youtube video into my website, an error message pops up: An error occurred. Please try again later. (Playback ID: 044VSn6cAs2PR3y3) Learn More In the code snippet from my index.html, this is what it looks like: <div c ...

Error message on Android Web Console: ReferenceError - Worker object is not defined

As someone who is new to javascript, I have been struggling to understand why popular browsers seem to handle the definition "new Worker("BarcodeWorker.js")" differently than Android WebView. The original code for this Barcode Reader is from Eddie Larsso ...

The Dreamweaver code is visible on the design page but does not appear in the live view on the browser

Experimenting with something and created the top section of the site (Logo and menu). However, when I tried to add a table with text, it doesn't appear in the browser preview even though I can see it in the CODE and DESIGN tabs of Dreamweaver. I susp ...

Unbelievable attribute: sup causing peculiar styling in link element

We've recently come across an issue where the text-decoration of an underline in an anchor tag appears strange when there is a sup element present. The visual glitch looks like this: For reference, here's the HTML structure we are working with: ...

What is the best way to verify the type of an object received from request.body in Typescript

Is it possible to check the object type from the request body and then execute the appropriate function based on this type? I have attempted to do so in the following manner: export interface SomeBodyType { id: string, name: string, [etc....] } ...

Ways to verify the nodemon version that is currently installed on your local machine

On my Windows 10 machine, I recently installed nodemon locally in a project and now I'm curious to know which version is installed. Can someone please share the command to check the version of nodemon without needing to install it globally? My aim is ...

Creating a well-aligned form using Material-UI

Exploring Material-UI for the first time! How can a form be built where certain fields are arranged horizontally, others stacked vertically, and all aligned perfectly both vertically and horizontally? Check out this example image: I've created simila ...

The for loop does not cycle through every element

I'm working on a class project that involves creating custom HTML tags with JavaScript. I have a for loop set up to iterate over each use of the tag, but for some reason, it only seems to loop over every other tag. I'm specifically trying to crea ...

Unable to change background-image as intended

Here is an example of my HTML code with an initial background image: <div id="ffff" style="width: 200px; height: 200px; background-image: url('/uploads/backroundDefault.jpg')">sddsadsdsa<br>dffdsdfs</div> Everything seems to b ...

Stop jQuery from resetting the background image when using fadeOut()

Currently, I am using fadeIn and fadeOut functions with jQuery to create a fading effect. However, when the final fade out occurs, the entire CSS table fades out and the background image of the CSS body comes into view. Despite setting the background-posit ...

Tips for updating values in a nested array within JSON

I am working with the following .json file and my goal is to update the values of "down" and "up" based on user input. "android": { "appium:autoAcceptAlerts": true, "appium:automationName": "UiAutomator2", ...

The jQuery.each function creates copies of items during every iteration

Having an issue with my JSON data display on a webpage. Each item appears to be duplicated with every loop iteration, even though the block variable seems correct when logged. I've tried searching for a solution online, but haven't had any luck ...

Adjusting padding evenly across an unspecified number of columns

I'm currently facing a challenge with a multi-column layout. In a basic setup of three columns, I aim to adjust the padding so that each gap between the columns is consistent (2rem). However, the tricky part lies in crafting rules that can be applied ...

Promises.all fails to reach the catch block after the initial rejection

I'm completely new to promises and I'm learning how to save multiple items to a MongoDB database system. When dealing with a single item, I have a function that utilizes promises. It returns a promise that either rejects if the database save ope ...

How can you target a specific data-target button while working with multiple Bootstrap collapse elements?

One challenge I'm facing is with the Bootstrap collapse elements on my website. I have several of them, each controlled by a read-more button that includes an icon to indicate the state of the collapse element. However, when I add a class to the butto ...

Retrieving Parts of a URL in AngularJS

Is there a way to extract all parts of a URL after "/"? For example: http://myadress.com/#/example/url/here/param1 Result: ['example', 'url', 'here']. Currently, $routeParams only returns an object with route parameters lik ...

Seeking a regular expression to identify special characters appearing at the beginning of a string

I'm looking to update my current regex pattern to include special characters at the beginning of a string value. Here's what I have right now: /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d][A-Za-z\d!@#$%^&*()_+.]{ ...

Search for "conditions" in a basic HTML DOM parser

I encountered an issue when trying to parse information from a website using simple HTML DOM parser. My code looks something like this: <li> <p class="x"></p><p>..</p> <p>..</p> <p>..</p> <p class ...

Submitting a form with AJAX and additional fields can be accomplished by including the extra fields in

Imagine a scenario where I am working with a basic form like the one below <%= form_for(@category) do |f| %> <% if @category.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@category.errors.count, "erro ...