我红了另外一篇文章,但没有意识到我的问题特别在哪里。我想我不会在提交时多次调用函数。这是一个联系表单,我想通过ajax
发送到控制器操作,数据将被验证,如果可以,将保存在数据库表中。你能告诉我方向我的错误吗?谢谢!
形式:
<form method="post" action="" onsubmit="contactProccess()">
<div class="panel panel-default">
<input id="form-token" type="hidden" name="<?=Yii::$app->request->csrfParam?>"
value="<?=Yii::$app->request->csrfToken?>" />
<div class="col-sm-12 contact-container">
<div class="panel-heading">
<h1 class="text-center contact-page-title <?=($message != "" ? " mt0 " : "")?>"><?= $page->title; ?></h1>
</div>
<div class="panel-body">
<div class="contact-form lg-margin h60">
<div class="text-input">
<input type="text" name="names" id="fancy-text" />
<label><?= Yii::t('app', 'app.Names') ?></label>
<div class="help-block"></div>
</div>
</div><!-- End .form-group -->
<div class="contact-form lg-margin h60">
<div class="text-input">
<input type="text" name="email" id="fancy-text"/>
<label>Email</label>
<div class="help-block"></div>
</div>
</div><!-- End .form-group -->
<div class="contact-form lg-margin h60">
<div class="text-input">
<input type="text" name="phone" id="fancy-text"/>
<label><?= Yii::t('app', 'app.Phone') ?></label>
<div class="help-block"></div>
</div>
</div><!-- End .form-group -->
<div class="contact-form lg-margin h60">
<div class="text-input">
<input type="text" name="title" id="fancy-text"/>
<label><?= Yii::t('app', 'app.Title') ?></label>
<div class="help-block"></div>
</div>
</div><!-- End .form-group -->
<div class="contact-form lg-margin h190">
<div class="text-input">
<textarea class="padding-message" name="message" id="fancy-text"></textarea>
<label><?= Yii::t('app', 'app.Message') ?></label>
<div class="help-block"></div>
</div>
</div><!-- End .form-group -->
<div class="contact-form lg-margin h100">
<?= '<img src="' . $_SESSION['captcha']['image_src'] . '" alt="CAPTCHA code" height="60" width="120">' ?>
<label><?= Yii::t('app', 'app.Security code') ?></label>
<input class="padding-message" name="captcha" id="fancy-text" />
<div class="help-block"></div>
</div><!-- End .form-group -->
<div class="xss-margin"></div><!-- space -->
<div class="contact-form submit-button">
<button type="submit"><?= Yii::t('app','app.Send') ?></button>
</div>
</div>
</div>
</div><!-- panel-end -->
</form>
的Ajax:
function contactProccess() {
var name = $('input[name="names"]');
var email = $('input[name="email"]');
var phone = $('input[name="phone"]');
var title = $('input[name="title"]');
var captcha = $('input[name="captcha"]');
var message = $('textarea[name="message"]');
$.ajax({
method: 'post',
url: '/admin/site/contactprocess',
data: {
name: name,
email: email,
phone: phone,
title: title,
message: message
},
success: function ( data ) {
return false;
}
});
return false;
}
此行动现在只是一种测试形式:
public function actionContactprocess(){
var_dump($_POST);die;
}
答案 0 :(得分:0)
您应该将click事件应用于提交按钮以制作ajax而不是使用obsubmit。为该按钮添加一个id并向其添加click事件
<form>
<div class="panel panel-default">
<input id="form-token" type="hidden" name="<?=Yii::$app->request->csrfParam?>"
value="<?=Yii::$app->request->csrfToken?>" />
<div class="col-sm-12 contact-container">
<div class="panel-heading">
<h1 class="text-center contact-page-title <?=($message != "" ? " mt0 " : "")?>"><?= $page->title; ?></h1>
</div>
<div class="panel-body">
<div class="contact-form lg-margin h60">
<div class="text-input">
<input type="text" name="names" id="fancy-text" />
<label><?= Yii::t('app', 'app.Names') ?></label>
<div class="help-block"></div>
</div>
</div><!-- End .form-group -->
<div class="contact-form lg-margin h60">
<div class="text-input">
<input type="text" name="email" id="fancy-text"/>
<label>Email</label>
<div class="help-block"></div>
</div>
</div><!-- End .form-group -->
<div class="contact-form lg-margin h60">
<div class="text-input">
<input type="text" name="phone" id="fancy-text"/>
<label><?= Yii::t('app', 'app.Phone') ?></label>
<div class="help-block"></div>
</div>
</div><!-- End .form-group -->
<div class="contact-form lg-margin h60">
<div class="text-input">
<input type="text" name="title" id="fancy-text"/>
<label><?= Yii::t('app', 'app.Title') ?></label>
<div class="help-block"></div>
</div>
</div><!-- End .form-group -->
<div class="contact-form lg-margin h190">
<div class="text-input">
<textarea class="padding-message" name="message" id="fancy-text"></textarea>
<label><?= Yii::t('app', 'app.Message') ?></label>
<div class="help-block"></div>
</div>
</div><!-- End .form-group -->
<div class="contact-form lg-margin h100">
<?= '<img src="' . $_SESSION['captcha']['image_src'] . '" alt="CAPTCHA code" height="60" width="120">' ?>
<label><?= Yii::t('app', 'app.Security code') ?></label>
<input class="padding-message" name="captcha" id="fancy-text" />
<div class="help-block"></div>
</div><!-- End .form-group -->
<div class="xss-margin"></div><!-- space -->
<div class="contact-form submit-button">
<button id="submit-btn"><?= Yii::t('app','app.Send') ?></button>
</div>
</div>
</div>
</div><!-- panel-end -->
</form>
这里有相同的js方法: -
$('#submit-btn').click(function(){
var name = $('input[name="names"]');
var email = $('input[name="email"]');
var phone = $('input[name="phone"]');
var title = $('input[name="title"]');
var captcha = $('input[name="captcha"]');
var message = $('textarea[name="message"]');
$.ajax({
method: 'post',
url: '/admin/site/contactprocess',
data: {
name: name,
email: email,
phone: phone,
title: title,
message: message
},
success: function ( data ) {
return false;
}
});
return false;
});