簡體   English   中英

php-ajax:如何在表單中的每個工作中使用兩個提交按鈕以實現不同的目的

[英]php-ajax: how to use two submit button on form every one work for different purpose

在發布此問題之前,我檢查了以下鏈接。 如何使用兩個提交按鈕,並區分使用哪個按鈕提交表單?

一種形式的兩個提交按鈕

但我仍然面臨問題。 以下是代碼。 HTML代碼

<form name="posting" id="posting" method="post" action="posting_bk.php" role="form">
    <input class="btn btn-home" type="submit" name="publish" id="publish" alt="Publish" value="Preview and Post" />
    <input class="btn btn-home" type="submit" name="save" id="save" onclick="return confirm('Are you sure you want to Submit.')" alt="Save" value="Save as Draft" /></center>
</form>

這是posting_bk.php上的PHP代碼

    if (isset($_POST['publish'])) {
        # Publish-button was clicked
array_push($res['errors'], 'Publish button');
    echo json_encode($res);
    }
    elseif (isset($_POST['save'])) {
      # Save-button was clicked
array_push($res['errors'], 'Save button.');
    echo json_encode($res);
    }

問題是我沒有得到任何輸出。 我本來是想進行測試發布測試保存的

我打算使用ajax,下面是我為ajax編寫的代碼。

<script type="text/javascript">
    $('document').ready(function() {
        $("#posting").on("submit", function(e) {
            e.preventDefault;
            var btn = $('#publish');

            $.ajax({
                type: 'post',
                url: $('form#posting').attr('action'),
                cache: false,
                dataType: 'json',
                data: $('form#posting').serialize(),
                beforeSend: function() {
                    $("#validation-errors").hide().empty();
                },
                success: function(data) {
                    if (data.success == false) {
                        var arr = data.errors;
                        $.each(arr, function(index, value) {
                            if (value.length != 0) {
                                $("#validation-errors").append('<div class="alert alert-danger"><strong>' + value + '</strong><div>');


                            }
                        });
                        $("#validation-errors").show();
                        btn.button('reset');
                    } else {
                        $("#success").html('<div class="alert alert-success">Basic details saved successfully. <br> If you want to edit then please goto <a href="edit.php">Edit</a>. <div>');
                        $('#title').val('');

                    }
                },
                error: function(xhr, textStatus, thrownError) {
                    alert('Something went to wrong.Please Try again later...');

                    btn.button('reset');
                }
            });
            return false;
        });
    });
</script>

如果我不使用ajax,那么它工作正常。 好像是Ajax的問題

您無法以嘗試的方式進行操作,因為無論單擊哪個按鈕,都將同時設置發布和保存。

您需要在客戶端執行此操作。 將事件偵聽器綁定到每個按鈕,該偵聽器將傳遞您想要的任何“操作”變量。

例如,如果您使用的是jQuery:

$('#publish').on('click', function(e){
   e.preventDefault();
   // Set some hidden form variable called action to a value of publish
   // submit form or make an ajax call.
});

$('#save').on('click', function(e){
   e.preventDefault();
   // Set some hidden form variable called action to a value of save
   // submit form or make an ajax call.
});

現在您的PHP可以檢查$ _POST ['action'] =='save'|'publish'並采取相應的措施。

我將點擊提交,將點擊按鈕的ID傳遞給php:

$('#posting input[type="submit"]').on("click", function(e) {                
                e.preventDefault;
                var btn = $('#publish');
                var el = $(this).attr('id');
                $.ajax({
                    type: 'post',
                    url:$('form#posting').attr('action'),
                    cache: false,
                    dataType: 'json',
                    data: {data:$('form#posting').serialize(),action:el},
                    beforeSend: function() { 
                        $("#validation-errors").hide().empty(); 
                    },
                    success: function(data) {
                        if(data.success == false)
                            {
                                var arr = data.errors;
                                $.each(arr, function(index, value)
                                {
                                    if (value.length != 0)
                                    {
                                        $("#validation-errors").append('<div class="alert alert-danger"><strong>'+ value +'</strong><div>');


                                    }
                                });
                                $("#validation-errors").show(); 
                                btn.button('reset');                            
                            } else {
                                $("#success").html('<div class="alert alert-success">Basic details saved successfully. <br> If you want to edit then please goto <a href="edit.php">Edit</a>. <div>');
                                $('#title').val('');

                            }
                    },
                    error: function(xhr, textStatus, thrownError) {
                        alert('Something went to wrong.Please Try again later...');

                        btn.button('reset');
                    }
                });             
                return false;
            });
        });

的PHP:

    if ($_POST['action'] == 'publish') {
            # Publish-button was clicked
echo 'test publish';
        }
        elseif ($_POST['action'] == 'save') {
            # Save-button was clicked
echo 'test save';
    }

暫無
暫無

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

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