Yii2 ActiveForm Ajax POST called twice

im newbie in Yii2. This is my first serious app.

The problem is my AJAX modal form sens POST twice.

This is how i calls modal form

                Modal::begin([
                    'header' => '<h2>Hello world</h2>',
                    'toggleButton' => ['label' => 'click me'],
                    'id' => 'order_form'
                ]);


                Modal::end();
                ?>

This is my JS code

$this->registerJs('
$(function() {
$( "#orderer_form" ).on( "submit", function( event ) {
  event.preventDefault();
  event.stopImmediatePropagation();
  var formData = $( this ).serialize() ;
   alert($("#orderer_form").serializeArray());
       $.ajax({
            type: "POST",
            url: "/order",
            data: formData,
            success: function(msg){
                $("#thanks").html(msg);
                $("#order_form").modal("hide"); 
            },
            error: function(){
            //alert("failure");
            }
        });
    return false;
});
});
');

This is how i render modal view

<?php echo \Yii::$app->view->renderFile('@app/views/common/order_form.php', array('model'=>new salestable()));?>

This is the result

enter image description here

Upvotes: 0

Views: 1111

Answers (1)

Jap Mul
Jap Mul

Reputation: 18769

Try changing the js to this, for me this always seems to fix the problem.

$('body').on('submit', '#orderer_form', function() {
    // Your code here
});

Upvotes: 1

Related Questions