簡體   English   中英

多個提交按鈕php不同的動作

[英]Multiple submit buttons php different actions

我有一個網站,我想在其中設置 2 個單獨的提交按鈕,其中一個按鈕將獲取輸入的數據並對其進行一些計算以顯示在同一屏幕上。 我已經成功地使用了這個:

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

然后我使用:

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

現在我想要第二個提交按鈕,該按鈕仍會抓取他們輸入的數據,但隨后會轉到不同的地址。 有沒有一種優雅的方法來做到這一點?

您可以向新的提交按鈕添加一個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>

您可以通過在 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>

注意:Internet Explorer 9 及更早版本不支持按鈕標記的 formaction 屬性。

處理多個提交按鈕的最佳方法是在動作腳本中使用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>

動作/服務器端腳本:

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;
}

?>

參考: W3Schools

另一種方法是您可以創建和使用一些會話變量來輕松實現它。

例如 $_SESSION['validate']。

按鈕的 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 和 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>

處理程序 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); 

?>

它可能不是最佳或有效的解決方案。 我的經驗水平並不高,所以我想出了這個解決方案,在所有可用的解決方案之后,它最符合我的目的,但有其局限性。 這不是任何地方都想在這里寫的。

注意:您可能會注意到它包括一些其他部分,例如演示文稿、腳本和后端文件之間的響應、成功和錯誤處理。

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM