简体   繁体   English

一种形式,两种动作,两种形式用php提交

[英]One form and two actions and two submit in php

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. 之后,选择pdf或excel生成首选格式。 在此处输入图片说明

The problem is the form, I want it to action generateExcel.php if excel is pressed and generatePdf.php if PDF is pressed. 问题是形式,如果要按excel,我希望它操作generateExcel.php,如果按PDF,则要generatePdf.php。 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>

It is possible to override the action attribute of the parent form using the HTML5 formaction attribute on a button. 使用按钮上的HTML5 formaction属性可以覆盖父表单的action属性。 See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button 参见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 浏览器支持看起来不错: 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. 但是,webeno的答案肯定会在所有浏览器中都有效,并且在一个文件中管理所有表单处理代码会更容易。

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 ). 编辑:如果您的2个文件太大(或者您出于任何其他原因想要将它们分开),您仍然可以使用include (或require -有关差异的更多信息: php中“ include”和“ require”之间的差异)。

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: 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): 或者,您可以仅在该单独的文件上使用redirect( generate.php确保此页面上没有其他内容):

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM