Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pjax with mutiple submit buttion #3719

Closed
dubeymahi48 opened this issue Jun 6, 2014 · 9 comments
Closed

Pjax with mutiple submit buttion #3719

dubeymahi48 opened this issue Jun 6, 2014 · 9 comments

Comments

@dubeymahi48
Copy link

I am using multiple submit button. I want name of submitted button name. I am have this code in controller
if (Yii::$app->request->getIsAjax() || Yii::$app->request->getIsPjax()) {
if ($model->load(Yii::$app->request->post())) {
echo $_POST['saveList'];}
else{
echo 'other';
}
}

This is my button
echo Html::submitButton($model->isNewRecord ? 'Save and Show List' : 'Update and Show List', ['name' => 'saveList', 'value' => '1', 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);

echo Html::submitButton($model->isNewRecord ? 'Save and Create New' : 'Update and View', ['name' => 'saveList', 'value' => '2', 'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);

@godardth
Copy link

Hi,

I have the same issue, I am using a pjax form with two submit buttons.
When data-pjax is set to 1 in my ActiveForm::begin call, the submit buttons names are not set anymore in the $_POST array.

Anyone has a solution for this issue ?

Thanks

Here is my view (form simplified):
$form = ActiveForm::begin($config = [
'action' => Url::toRoute('display-confirm'),
'options' => ['class' => 'form-pers' ], 'data-pjax'=>'1'],
]);
echo Html::submitInput('Sell', ['name' => 'sellbtn',]);
echo Html::submitInput('Buy', ['name' => 'buybtn',]);
ActiveForm::end();

And my controller:
public function actionDisplayConfirm(){
return var_dump($_POST);
}

@Faryshta
Copy link
Contributor

I think this should be moved to the yii2-pjax repository since it looks like a javascript issue.

can @yii-bot do it?

@cebe
Copy link
Member

cebe commented Apr 10, 2016

@Faryshta the @yii-bot currently only works for extensions. also pjax issues may affect core and/or pjax repo so generally adding the label may not qualify for a move. Just open an issue there referring to this one. Thanks for checking this!

@Faryshta
Copy link
Contributor

https://jsfiddle.net/q0f5x6rh/ i built this fiddle. Looks like the FormData class for javascript works fine meanwhile jQuery.serializeArray() ignores the clicked button.

I was searching for solutions on how to know which submit button triggered the event and all of them imply rewritting the way the click event is handled for the <button> element. http://stackoverflow.com/questions/3577469/form-onsubmit-determine-which-submit-button-was-pressed

and finally i searched for which browsers support the FormData javascript class https://developer.mozilla.org/en-US/docs/Web/API/FormData it seems this is an issue for internet explorer 9 at the moment.

we should tag this as "wont fix" and add on the docs how to work around this using click events in my opinion.

the other solution implies a rewrite for jquery-pjax events and even create new ones.

@SilverFire
Copy link
Member

I didn't test it, but why does the execution come to that serializeArray() in case of POST form? I think it should not.
Or there s something magical with window.FormData !== undefined?

@Faryshta
Copy link
Contributor

@SilverFire I provided a jsfiddle so you just need to run it.

Basically FormData is a class introduced in HTML5 which simplifies ajax handling with forms. It allows file uploads and knowing which button value triggered the submit.

So if the class FormData is undefined, then the best way to pass data to the ajax is with serializeArray() which lacks functionalities.

@SilverFire
Copy link
Member

Now I get it, thank you for detailed explanation. I'm OK with tagging this issue as wont.
Could you update your PR #11316 and add a comment about this, please?

@Faryshta
Copy link
Contributor

jquery/jquery#2321 jquery won't fix it either. Looks like the only 'solution' is to drop support for IE 9 and alike.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants