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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:54:26+00:00 2026-05-27T12:54:26+00:00

On my page I’m using a form which on submit should get some extra

  • 0

On my page I’m using a form which on submit should get some extra hidden fields. The problem however is that I can’t know the name/id of the form nor the submit button so I can’t use functions like onClick etc. Therefore I’m using the beforeunload event to trigger an event when the user tries to leave the page (this includes submitting a form). I’m using the document.activeElement to find out what triggered the event and if it’s a form/submit I’m adding the hidden fields to the form.

Stripping down the code it comes down to something like this:

var javascriptData = ['val1','val2','val3']; // usually gets values dynamically and can differ in size
$(window).bind('beforeunload', function(e) {
    var activeForm = document.activeElement.form;
    for(var i = 0; i < javascriptData.length; i++){
        $(activeForm).append('<input type="hidden" name="test-data[]" value="'+javascriptData[i]+'" />');
    }
    return false; // just for debug purposes.
}

The code works and the element is added to the form but it doesn’t show up in the $_POST on the next page. However, the final line of code (return false) creates a pop-up asking if I want to leave the page, if I stay on the page, press the submit button again and then leave the page and check $_POST, the field there. My guess is that the data for the $_POST is collected before the beforeunload function is triggered.

My question is: “Is there a way around this so I can add a hidden field to a form in the beforeunload event?”.

Help is much appreciated, thanks in advance.

  • 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-27T12:54:26+00:00Added an answer on May 27, 2026 at 12:54 pm

    Like the comments above mentioned, you can just hook onto the submit event.

    $('form').submit(function() {
        for(var i = 0; i < javascriptData.length; i++){
            $(this).append('<input type="hidden" name="test-data[]" value="'+javascriptData[i]+'" />');
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
My page should contain a link that looks like <a href=/desired_path/1>Click to go</a> .
Page one contains an HTML form. Page two - the code that handles the
Page has menu items that would replace a 'div id=foo_(current menu item)' with 'div
page contents: aa<b>1;2'3</b>hh<b>aaa</b>.. .<b>bbb</b> blabla.. i want to get result: 1;2'3aaabbb match tag is
This page displays beautifully in firefox but i get all kinds of problems when
My page scrolls down a bit, so the problem is when the ASP.NET validation
Page has image with hyperlink and that hyperlink has target=_blank and every time i
Page - http://blu-eye.com/index.html - contains suckerfish menu which is displaying correctly on the rest
page.getByXPath(//*[@href='http://www.example.com/index.do/abc/1_*'); Do I need to escape any characters? I am trying to get all

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.