Вопрос

I'm using Yii as a PHP framework for my site. Additionally, my site uses some js/jquery like, say, a jQuery UI Dialog widget (except for those dialogs, the rest of the code is pure normal html form components and jQuery code for the event handlers).

In the Yii side, I use CForms to build my forms from specifications file.

When I test if the form was submitted, I must do it for a certain button. This is not only forced, but I also take advantage of it.

if ($myCFormInstance->submitted('approve')) {
    //process approval code
} else if ($myCFormInstance->submitted('reject')) {
    //process rejection code
}

The actual problem I have is a bit conceptual one, since -fortunately- I know what's going on with my code and -again, fortunately- know the problem root:

Somewhere in My code I intercept the submit button's click event:

$(function(){
    $(".critical-action").click(function(e){
        var form = $(this).closest("form");
        e.preventDefault();
        e.stopImmediatePropagation();
        confirmDialog("¿Continuar?", "#critical-action-dialog", function(){
            form.submit();
        });
    });
});

Say the .critical-action classed elements are always a submit button in a form.

The intention of the code: cancel the form submission, and perform it only if the user -in the dialog- clicks the "Yes, Continue" (i.e. confirming the action) button.

This code works as expected, and have no problems at a javascript level BUT -and here goes my issue- when doing form.submit(), the button is not sent as part of the form. This is obvious: I'm sending the form without specifying any button. In the case of Approve and Reject, which have two buttons, the example explains itself: if the form.submit() call could send their buttons ¿which of them should send?.

Question: So, since form.submit() doesn't send any button, but I actually need buttons ¿how can I send the form "with the corresponding button" -i.e. a button I choose to specify, which should correspond to this in the click handler function context- automatically via javascript? The button NEEDS to be identified by Yii in order to process the form (specially with the Approve and Reject case).

Это было полезно?

Решение

If you added a hidden input to the form, you can modify the input value with jQuery before you submit the form, like this:

$("#inputID").val('approve');

If you want to set the value to the value of the clicked button via $(this).val(), be aware of the issue that could result in an IE browser, explain here. The second answer (by postpostmodern) has a solution to this issue.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top