Multiple submit buttons php different actions

后端 未结 4 1339
悲&欢浪女
悲&欢浪女 2020-11-30 05:56

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. I\

相关标签:
4条回答
  • 2020-11-30 06:09

    You could add an onclick method to the new submit button that will change the action of the form and then submit it.

    <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>
    
    0 讨论(0)
  • 2020-11-30 06:15

    Another approach is that You can create and Use some session variable to achieve it easily.

    E.g. $_SESSION['validate'].

    HTML and PHP Code for buttons

    <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

    <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
    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!

    0 讨论(0)
  • 2020-11-30 06:16

    The best way to deal with multiple submit button is using switch case in action script

    <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

    <?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

    0 讨论(0)
  • 2020-11-30 06:20

    You can change the form action by using formaction="page1.php" in button property .

    <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.

    0 讨论(0)
提交回复
热议问题