Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have one submit button in a form (out of multiple buttons) open a new tab

Tags:

html

forms

php

I have a form with a button to submit and a button to preview a PDF before submitting. I'd like the preview PDF button to pass the form data to a new page (which will obviously generate the PDF), but the submit button to work on the current page.

I've been looking around but most answers point to using target="_blank" in the form tags, though that won't work in my case as that would open every button in a tab. Some others show how to open a window in javascript, but without any form data unless it was passed through the URL.

I only really know PHP and HTML, so I could potentially do it by submitting to the same page, replacing the text in the boxes, and opening a new window in the process, but it'd be a bit overcomplicated just for the sake of a new tab, plus I'd either need to set everything as session variables or pass things through the URL.

like image 645
Peter Avatar asked Dec 03 '25 11:12

Peter


1 Answers

Since there didn't seem to be any apparent easy ways of doing such a simple thing, I reworded the question a lot and eventually found something. Turns out there was a super simple way I'd totally missed.

After trying mplugjan's answer, I realised the only easy way would be to have two separate forms for each button (unless I did akshay's suggestion, but that was a last resort), but I'd somehow need to get the values from the main form, which is impossible from a separate form as far as I know. Then I figured I only actually needed the buttons to have different actions, which fortunately did come up with results. It can be done through formaction and formtarget.

Brief example of how to do it in case anyone else comes across this:

<form action = "page.php">
    <input type="submit" value="Accept"/>
    <input type="submit" value="Preview PDF" formaction="pdf.php" formtarget="_blank"/>
</form>
like image 79
Peter Avatar answered Dec 05 '25 01:12

Peter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!