简体   繁体   English

多个提交按钮php不同的动作

[英]Multiple submit buttons php different actions

I have a website started where I want to have 2 separate submit buttons, one of which will take data entered and do some calculations to it to display on the same screen.我有一个网站,我想在其中设置 2 个单独的提交按钮,其中一个按钮将获取输入的数据并对其进行一些计算以显示在同一屏幕上。 I've got this successfully working with:我已经成功地使用了这个:

<form id="form1" name="form1" method="post" onsubmit="" onreset="" action="programname.php">
<input type="submit" name="calc" value="Find Angle">

and then I use:然后我使用:

if (!isset($_POST['submit'])){
Do actions, display calculations}

Now I want a second submit button that still grabs the data they entered but then goes to a different address.现在我想要第二个提交按钮,该按钮仍会抓取他们输入的数据,但随后会转到不同的地址。 Is there an elegant way to do this?有没有一种优雅的方法来做到这一点?

You could add an onclick method to the new submit button that will change the action of the form and then submit it.您可以向新的提交按钮添加一个onclick方法,该方法将更改表单的操作,然后提交它。

<script type="text/javascript">
  function submitForm(action) {
    var form = document.getElementById('form1');
    form.action = action;
    form.submit();
  }
</script>

...

<form id="form1">
  <!-- ... -->
  <input type="button" onclick="submitForm('page1.php')" value="submit 1" />
  <input type="button" onclick="submitForm('page2.php')" value="submit 2" />
</form>

You can change the form action by using formaction="page1.php" in button property .您可以通过在 button 属性中使用 formaction="page1.php" 来更改表单操作。

<form id="form1" name="form1" method="post" onsubmit="" onreset="" action="programname.php">
    <input type="submit" name="calc" value="Find Angle">

    <input type="button" type="submit" formaction="page1.php">Action 0</button>
    <input type="button" type="submit" formaction="page2.php">Action 1</button>
</form>

Note: The formaction attribute of the button tag is not supported in Internet Explorer 9 and earlier versions.注意:Internet Explorer 9 及更早版本不支持按钮标记的 formaction 属性。

The best way to deal with multiple submit button is using switch case in action script处理多个提交按钮的最佳方法是在动作脚本中使用switch case

<form action="demo_form.php" method="get">

    Choose your favorite subject:

    <button name="subject" type="submit" value="html">HTML</button>
    <button name="subject" type="submit" value="css">CSS</button>
    <button name="subject" type="submit" value="javascript">Java Script</button>
    <button name="subject" type="submit" value="jquery">jQuery</button>

</form>

Action / Server Side script:动作/服务器端脚本:

demo_form.php demo_form.php

<?php

switch($_REQUEST['subject']) {

    case 'html': //action for html here
                break;

    case 'css': //action for css here
                break;

    case 'javascript': //action for javascript here
                        break;

    case 'jquery': //action for jquery here
                    break;
}

?>

Ref: W3Schools参考: W3Schools

Another approach is that You can create and Use some session variable to achieve it easily.另一种方法是您可以创建和使用一些会话变量来轻松实现它。

Eg $_SESSION['validate'].例如 $_SESSION['validate']。

HTML and PHP Code for buttons按钮的 HTML 和 PHP 代码

<button type="submit" id="first_submit" style="<?php echo isset($_SESSION['validate'])?'display:none':'';?>">first submit</button>
<button type="submit" id="second_submit" style="<?php echo isset($_SESSION['validate'])?'':'display:none';?>">second submit</button>

jquery and ajax Script jquery 和 ajax 脚本

<script>
    $(document).ready(function(){

        $("#form").on('submit', function(e){
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: 'handler-file.php',
                data:  new FormData(this),
                dataType: "json",
                enctype: 'multipart/form-data',
                contentType: false,
                cache: false,
                processData:false,
                error:function(error){
                    //your required code or alert
                    alert(error.responseText);
                },
                success: function(response){
                    if(response.status=='1')
                    {
                        //your required code or alert
                        $('#first_submit').hide();
                        $('#second_submit').show();
                    }
                    else if(response.status=='2')
                    {
                        //your required code or alert
                        $('#first_submit').show();
                        $('#second_submit').hide();
                    }
                    else
                    {
                        //your required code or alert
                    }
                }
            });
        });

    });
    </script>

Handler PHP File处理程序 PHP 文件

<?php
session_start();

$result['status']='0';
$result['error']='';

if(!isset($_SESSION['validate']))
{
    if(!isset($_FILES['file'])) 
    {
        $result['error'].='[Er-02 file missing!]';
    }
    else
    {
        //your other code
        $_SESSION['validate'] = true;
        $result['status']='1';
    }
}
else if($_SESSION['validate']==true)
{
    if(!isset($_FILES['file'])) 
    {
        $result['error'].='[Er-03 Validation file missing!]';
    }
    else
    {
        //your other code
        unset($_SESSION['validate']);
        $result['status']='2';
    }
}
else
{
    $result['error'].='[Er-01 Invalid source!]';
}
echo json_encode($result); 

?> ?>

It may not be the optimal or efficient solution.它可能不是最佳或有效的解决方案。 My level of experience is not too much, so I came up with this solution what served my purpose best after all the solutions available but with their limitations.我的经验水平并不高,所以我想出了这个解决方案,在所有可用的解决方案之后,它最符合我的目的,但有其局限性。 This was not anywhere so thought to write it here.这不是任何地方都想在这里写的。

Note: You may notice it includes some other parts like response, success and error handling between presentation, script and backend file.注意:您可能会注意到它包括一些其他部分,例如演示文稿、脚本和后端文件之间的响应、成功和错误处理。

Hope it helps!希望能帮助到你!

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

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