submitButton not working with Pjax

So for some odd reason, I can’t get Pjax to work with ActiveForm on the submit button.

If I change the submit button to a simple link, then no problem, but of course, the form doesn’t get submitted. Using the submit button, the pjax doesn’t trigger and I get this following error in the console.

Uncaught $.fn.pjax or $.pjax.click requires an anchor element

Here’s my code.


<?php Pjax::begin(['linkSelector' => '#testpjax', 'enablePushState' => false]) ?>

<div class="new-line">


    <?php $form = ActiveForm::begin(['action' => 'test', 'options' => ['data-pjax' => '']]); ?>


        <?= $form->field($model, 'line')->textarea()->label('') ?>

    

        <div class="form-group">

            <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'id' => 'testpjax']) ?>

        </div>

    <?php ActiveForm::end(); ?>


</div>

<?php Pjax::end() ?>

I believe your error message revolves around trying to use linkSelector to select a button(ie it is not a link) to trigger the Pjax form submit. This means the Pjax will not submit the form because it is looking for a link that does not exist. Try removing that part from your code, and I think your form will start to submit with Pjax if you change the forms data-pjax attribute to be true.

[b]

[/b]




<?php 

	// remove LinkSelector to stop looking for non existent link to trigger Pjax

	Pjax::begin(['enablePushState' => false]) 

?>


<div class="new-line">

 	

    <?php 

 	// By default, any link click or form submission (for those forms with data-pjax attribute) within the body content will trigger an AJAX request.

	$form = ActiveForm::begin(['action' => 'test', 'options' => ['data-pjax' => true]]); ?>


        <?= $form->field($model, 'line')->textarea()->label('') ?>

    

        <div class="form-group">

            <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'id' => 'testpjax']) ?>

        </div>

    <?php ActiveForm::end(); ?>


</div>

<?php Pjax::end() ?>



References:

http://www.yiiframew…dgets-pjax.html

http://www.yiiframew…Selector-detail

http://www.yiiframew…Selector-detail

That did it! Thank you!

I’m really surprised I wasn’t able to figure this out. This isn’t the first time I’ve used Pjax in my project and I could have swore I tried what you suggested… either way. It’s working now and I really appreciate it!

Hi I am facing issue with pushstate, I have passed <?php yii\widgets\Pjax::begin([‘enablePushState’ => false]) ?>

but its still display whole parameters in Url