Wizard
Wizard

Reputation: 11295

Yii CHtml::link() example

I have form and 2 CHtml::link() with different url.

My form method is get.

What I want: when click in 1 CHtml::link() - submit form to example.com/first using method get What I want: when click in 2 CHtml::link() - submit form to example.com/second using method post

Does is this possible ? I mean that I need change form method for different submit button and actions.

Upvotes: 0

Views: 241

Answers (1)

Goodnickoff
Goodnickoff

Reputation: 2267

You can submit form from javascript code:

$('#myLink1', '#myLink2').on('click', function(e){
    e.preventDefault();
    var method = $(this).attr('href')=='example.com/first' ? 'GET':'POST';
    $('#myFrom').attr(
        'action', 
        $(this).attr('href') //href attribute should contain appropriate url
    ).attr(
        'method', 
        method
    ).submit();
});

Also you can use jquery form plugin for sending form in ajax manner:

$('#myLink1').on('click', function(e){
    e.preventDefault();
    $('#myForm').ajaxSubmit({
         url: $(this).attr('href'),
         type: 'GET',
         success: function(){/*your code here*/}
    });
});
$('#myLink2').on('click', function(e){
    e.preventDefault();
    $('#myForm').ajaxSubmit({
         url: $(this).attr('href'),
         type: 'POST',
         success: function(){/*your code here*/}
    });
});

Upvotes: 1

Related Questions