tel2 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm not sure how Perlish this will work out to be, but Iíve got an existing Perl CGI application which contains a form, which when submitted with the appropriate values, redisplays that same form (as per the form action).  That part works fine, but Iíd like to make it so the submit opens a separate window (browser tab) at the same time, and displays a table in it.  The source of the HTML for that table is in a variable (say $table), not in a file.

The user would then either copy data from that table on the new window to the clipboard, or print the webpage, and when finished the user would close the window leaving them with the original window (which contains the form) only.

(One reason I want a new window is, it has to contain the table only, because I don't want any thing else like navigation controls being included in any printout, and I don't want the user to have to click the browser's back button to get back to the main window.)

I know I could write the HTML in $table out to a file, then display a link to that file (e.g. <a href="table.txt" target="_blank">Click for printable table</a>) on the main window after the submit, which the user could then click, but thatís an extra step for the user which I definitely want to avoid, which is why Iím asking for help.

Iíd also prefer not to write the table to a file, mainly because then I have to somehow delete it after itís been used, which is just more work, but Iím flexible on this.

Any suggestions on how to achieve the above, please?

I'm not wanting any jQuery solutions, but I might be willing to tolerate a bit (or even a byte) of plain JavaScript if required.  I'm using Perl's CGI module, so maybe it has something which could help.

I've also posted this question here: e-e.com/Q_29195399

Thanks. tel2

Replies are listed 'Best First'.
Re: Open new window after form submit
by afoken (Canon) on Sep 22, 2020 at 19:26 UTC
      Thanks Alexander.

      And yes, I use a PDF virtual printer too, and I rarely physically print anything, but sometimes others want a hard copy.

Re: Open new window after form submit
by trwww (Priest) on Sep 22, 2020 at 12:22 UTC
      Good point, trwww!  I didn't realise that.

      Much appreciated, thank you!

Re: Open new window after form submit
by NetWallah (Canon) on Sep 22, 2020 at 15:42 UTC
    One possibility is to embed the table - and provide a button to pop that up in a new window.

    An example is this stackoverflow item:
    https://stackoverflow.com/questions/10758442/embedded-code-to-be-displayed-on-click-event-of-button.

    You could even auto-pop it by triggering a button click when your new form is displayed (onLoad).

                    "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

      Thanks NetWallah.
Re: Open new window after form submit
by perlfan (Vicar) on Sep 22, 2020 at 19:52 UTC
    In keeping with your "old school" pattern, what you need to do first is create an endpoint that is called in the new tab that displays what you want. You'd have to have some form of persistence on the backend server so that it knows what values to display. So the flow looks like this:
    1. Visitor arrives to form and establishes a "session"
    2. User submits form to "action" URL
    3. "action" URL accepts request, saves response to the database; updates the "session" with an identifier
    4. onSubmit, JavaScript does it's thing and opens up display endpoint in a new tab
    5. the "session" id is communicated via the request
    6. secondary endpoint uses session to select what response data needs to be displayed
    7. output table is generated and response is sent to the browser

    OTOT, the "modern" approach would be to do effectively the same thing; but all in the same window and using an API call that returns structure data of what should be rendered after "submit" and some JS/CSS magick. You could also do effectively the same thing by skipping the secondary call and just using what was entered into the form as the source of the final view.

      Thanks perlfan.

      tel2
      Another Perl fan.

      OTOT

      On the other... Off the other topic? :-D