这是问题所在。我正在尝试使用ActiveForm和Braintree托管字段实现我的结帐表单的Ajax提交。处理Ajax的JS脚本已经过测试,它在我网站的其他部分工作正常。
使用此代码,我可以在我的控制器上获取$ _POST数据并使用它来继续正常的支付交易。
问题是。我的控制器操作一直到达脚本末尾的渲染点。这使我的页面重新加载。
我错了,或者我可以假设在这种情况下,在提交帖子后回复永远不会被触及?请求转到控制器,执行其操作,作为对视图的响应返回,不会发生任何其他事情。
在我发布流程的相关部分之后。 如果你可以提供帮助,我将非常感激。
控制器:
public function actionCheckout() {
$checkoutForm = new CheckoutForm();
$countriesList = $checkoutForm->countriesList();
$brainConfig = new BrainHelper();
$clientToken = $brainConfig->clientToken;
if ($checkoutForm->load(Yii::$app->request->post()) && $checkoutForm->validate()) {
$checkoutForm->value = BrainHelper::$boxPrice * $checkoutForm->quantity;
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$nonce = $_POST["payment_method_nonce"];
$brainHelper = new BrainHelper();
$result = $brainHelper->registerPayment($nonce);
if ($result->success) {
$this->saveClientPurchaseDataToDB($_POST, $checkoutForm->value);
BrainHelper::settlePayment($result->transaction->id);
} else { // In this case, the transaction wasn't authorised by Braintree. Such a pity!
// So let's communicate the bad news.
}
}
return $this->render('checkout', ['model' => $checkoutForm, 'clientToken' => json_encode($clientToken), 'countries' => $countriesList]);
}
观点:
<div class="checkout">
<?php $form = ActiveForm::begin(['id' => $model->formName(), 'enableClientValidation'=> true]);?>
<?= $form->field($model, 'first_name')->textInput(['placeholder' => Yii::t('checkoutForm','First Name')])->label(false) ?>
<?= $form->field($model, 'last_name')->textInput(['placeholder' => Yii::t('checkoutForm','Last Name')])->label(false) ?>
<?= $form->field($model, 'email')->textInput(['placeholder' => Yii::t('checkoutForm','Email')])->label(false) ?>
<?= $form->field($model, 'quantity') ?>
<label for="card-number">Credit card number:</label>
<div id="card-number"></div>
<label for="cvv">CVV code: <span>(3 or 4 digits behind your card)</span></label>
<div id="cvv"></div>
<label for="expiration-date">Card Expiration Date:</label>
<div id="expiration-date"></div>
<div class="form-group">
<div id="submission-btn">
<?= Html::submitButton(Yii::t('checkoutForm', 'Submit')) ?>
<?php ActiveForm::end(); ?>
</div>
<?php $script = <<< JS
$('form#{$model->formName()}').on('beforeSubmit', function(e) {
var \$form = $(this);
$.post(
\$form.attr("action"),
\$form.serialize() // serialize yii2 form
);
$(\$form).trigger("reset"); // cleans the form
return false;
});
JS;
$this->registerJs($script);
?>
提前致谢。