我有一个教程中的代码并不能很好地运行。我不知道为什么,我是codeigniter的初学者。我想提交按钮更新,如果重复文本将在电子邮件存在的文本框下方响应,并检查电子邮件是否是有效的电子邮件语法。
这是我的HTML代码:
<form id="reg_form" class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-3" for="curEmail">New Email</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="email" id="email" placeholder="youremail@yourcompany.com" required data-fv-emailaddress-message="The value is not a valid email address">
</div>
<div class="col-sm-8 ajax_response_result"></div>
</div>
<button onclick="editEmail()" class="btn btn-success btn-sm "><i class="fa fa-refresh"></i> Update Profile</button>
</form>
这是我的ajax:
function editEmail() {
jQuery.ajax({
type: "POST",
url: "<?php echo site_url('manager/profile/check_email') ?>",
data: $("#reg_form").serialize(),
success: function(res) {
$(".ajax_response_result").html(res);
}
});
}
我的CodeIgniter控制器:
public function check_email() {
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[user.user_email]|valid_email');
$this->form_validation->set_message('is_unique', 'The %s is already taken');
if ($this->form_validation->run() == FALSE):
echo 'Enter valid email.';
else :
unset($_POST);
echo 'Available';
endif;
}
答案 0 :(得分:2)
您的代码中有几个问题,修订版&amp;如下所示
HTML(方法POST
不在你的代码中,editEmail在这里event
缺少等等)
<form id="reg_form" class="form-horizontal" method="POST">
<div class="form-group">
<label class="control-label col-sm-3" for="curEmail">New Email</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="email" id="email" placeholder="youremail@yourcompany.com" required data-fv-emailaddress-message="The value is not a valid email address">
</div>
<div class="col-sm-8 ajax_response_result"></div>
</div>
<button onclick="editEmail(event)" class="btn btn-success btn-sm "><i class="fa fa-refresh"></i> Update Profile</button>
</form>
现在JS(e.preventDefault();
用于阻止表单提交等)
function editEmail(e) {
e.preventDefault();
jQuery.ajax({
type: "POST",
url: "<?php echo site_url('manager/profile/check_email') ?>",
data: $("#reg_form").serialize(),
success: function(res) {
$(".ajax_response_result").html(res);
}
});
}