Question

Wokring on a project where the user have the ability to chose from date and to date, and then chose one of the radio buttoms. After that chose pdf or excel to generate the to preferred format. enter image description here

The problem is the form, I want it to action generateExcel.php if excel is pressed and generatePdf.php if PDF is pressed. This is how far I have came and not working yet:

<form action='generatePdf.php' method='Post'/>

Fra Dato: <input type="text" name="fraDato" value="<?php echo date('d-m-Y'); ?>" /> 
Til Dato: <input type="text" name="tilDato" value="<?php echo date('d-m-Y'); ?>"> <br> 
<input type="radio" name="hent" value="timesmaling">Times malinger<br>
<input type="radio" name="hent" value="tredjetimesmaling">Tredje times malinger <br>
<input type="radio" name="hent" value="oppgaver">Oppgaver <br>
<input type="radio" name="hent" value="dagvakt">Dagvakt <br>
<input type="radio" name="hent" value="kveldsvakt">Kveldsvakt <br>
<input type="radio" name="hent" value="kontrollcm">Kontroll CM <br>

<input type='submit' name='pdf' value='PDF'>

<form action='generateExcel.php' method='Post'/>
<input type='submit' name='excel' value='excel'>

</form>
Was it helpful?

Solution

It is possible to override the action attribute of the parent form using the HTML5 formaction attribute on a button. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button

<input type='submit' name='pdf' value='PDF' formaction='generatePdf.php'>
<input type='submit' name='excel' value='excel'  formaction='generateExcel.php'>

The browser support looks pretty good: http://www.wufoo.com/html5/attributes/13-formaction.html

However, webeno's answer would definitely work in all browsers and it can be easier to manage all your form processing code in one file.

OTHER TIPS

I'd recommend you to put both of your scripts on the same file and validate against the button that has been clicked.

EDIT: If your 2 files are too big (or you would like to keep them separate for any other reason), you could still use include (or require - more info on the differences: Difference between "include" and "require" in php).

original file:

<form action='generate.php' method='Post'> <!-- removed the slash from the end here -->

Fra Dato: <input type="text" name="fraDato" value="<?php echo date('d-m-Y'); ?>" /> 
Til Dato: <input type="text" name="tilDato" value="<?php echo date('d-m-Y'); ?>"> <br> 
<input type="radio" name="hent" value="timesmaling">Times malinger<br>
<input type="radio" name="hent" value="tredjetimesmaling">Tredje times malinger <br>
<input type="radio" name="hent" value="oppgaver">Oppgaver <br>
<input type="radio" name="hent" value="dagvakt">Dagvakt <br>
<input type="radio" name="hent" value="kveldsvakt">Kveldsvakt <br>
<input type="radio" name="hent" value="kontrollcm">Kontroll CM <br>

<input type='submit' name='pdf' value='PDF'>
<input type='submit' name='excel' value='excel'>

</form>

generate.php:

if (isset($_POST['pdf'])) {
    include('generatePdf.php');
}

if (isset($_POST['excel'])) {        
    include('generateExcel.php');
}

EDITED

Alternatively you could just use redirect on that separate file (generate.php - make sure there is nothing else on this page):

if (isset($_POST['pdf'])) {
    header('Location: generatePdf.php');
}

if (isset($_POST['excel'])) {        
    header('Location: generateExcel.php');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top