简体   繁体   English

如何为具有不同操作的表单包括两个提交按钮

[英]how to include two submit buttons for a form with different action

Here is my scenario : 这是我的情况:

I have a html form on my main page where user can enter the data and and submitting the form using post method. 我的主页上有一个html表单,用户可以在其中输入数据并使用post方法提交表单。

<form  id="main" name="main" action="#text" method="post" > 


    //codes goes here


    <input  id="generate" type="submit"  name="script" value="create output" />

</form>

And the PHP code to process above form is 而要处理上述形式的PHP代码是

     <?php 

echo '<form action="#text" method="post">'; 

echo '<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly>';

//above form inputs will echo in this textarea

<-------PHP codes for download here---->
<-------PHP codes for email here---->


<input type="submit" id="download"  value="Download" name="download"></input>
<input type="submit" id="send"  value="send" name="send"></input>

echo '</textarea>';
echo '</form>';
?>

I am echoing the form output to a textarea and I need to have a download button a email button 我将表单输出回显到textarea,我需要有一个下载按钮和一个电子邮件按钮

to save\\send the textarea. 保存\\发送文本区域。 Here is issue I am facing is both the submit buttons on the second page executing download php function, not email. 我面临的问题是第二页上的两个提交按钮都执行下载php功能,而不是电子邮件。

So, How can I assign two separate functions for two submit functions ? 因此,如何为两个提交功能分配两个单独的功能? download and email ? 下载并通过电子邮件发送?

I am not using separate php page here instead of that using php code on the same page where I add html. 我不在这里使用单独的php页面,而不是在添加html的同一页面上使用php代码。

Buttons are only added to the $_POST when and if they are pressed, and as you can only press one button at a time, 仅当按钮被按下时,按钮才会添加到$ _POST中,并且由于您一次只能按下一个按钮,

you can just do this or something like it:- 您可以执行此操作或类似操作:-

if ( isset( $_POST['download'] ) ) {
    do_dowload_stuff();
}

if ( isset( $_POST['email'] ) ) {
    do_email_stuff();
}

Change your code to this: 将代码更改为此:

if(isset($_POST['download'])){
    // download code here
    echo "Download ".$_POST['output_textarea'];
} elseif(isset($_POST['send'])){
    // email code here
    echo "Email ".$_POST['output_textarea'];
}


echo '<form action="#text" method="post">'; 
    echo '<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly></textarea>';
    echo '<input type="submit" id="download"  value="Download" name="download"/>';
    echo '<input type="submit" id="send"  value="send" name="send"/>';
 echo '</form>';
?>

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

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