【发布时间】:2016-12-14 06:57:48
【问题描述】:
我正在尝试使用 codeigniter 和 ajax 验证注册表单,但我在提交表单后收到空响应。
这是注册过程的控制器。
public function individual_process(){
$full_name = $this->input->post('full_name');
$email_address = $this->input->post('email_address');
$password = $this->input->post('password');
$cpassword = $this->input->post('cpassword');
$d_o_b = $this->input->post('d_o_b');
$gender = $this->input->post('gender');
// form validation rules
$this->form_validation->set_rules('full_name', 'Full Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Primary Email', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required');
$this->form_validation->set_rules('d_o_b', 'Date of Birth', 'trim|required');
$this->form_validation->set_rules('gender', 'Gender', 'trim|required');
// $this->form_validation->set_rules('g-recaptcha-response','Captcha','callback_recaptcha');
$this->form_validation->set_error_delimiters('<div class="error" style="color:red; margin-top:5px; ">', '</div>');
if ($this->form_validation->run() == FALSE){
foreach($_POST as $key => $value){
$data['error'][$key] = form_error($key);
}
}else{
$data['success'] = true;
}
echo json_encode($data,true);
}
这里是ajax脚本
$("#step_submit").click(function(){
var full_name = $("#full_name").val();
var email_address = $("#email_address").val();
var password = $("#password").val();
var cpassword = $("#cpassword").val();
var gender = $("#gender").val();
$.ajax({
url: "<?php echo base_url(); ?>account/individual_process",
type: "post",
data: {full_name : full_name, email_address : email_address, password : password, cpassword : cpassword, gender : gender},
dataType: 'json',
async: false,
success: function (response) {
if(response.success == true){
location.reload();
}else{
//alert('error');
$(".error").html(' ');
$.each(response.error, function(key, value){
var element = $('#' + key);
element.closest('div.form-group')
.removeClass('has-error')
.addClass('has-error')
.find('.error').remove();
element.after(value);
});
}
},
});
});
【问题讨论】:
-
控制器名称“account”或“Account”是什么?
-
@kashif 其“帐户”
-
但是您使用的是 url: "account/individual_process", in ajax ("account" 而不是 "Account")
-
@kashif 有关系吗?
-
不,不是。但我们需要遵守约定。你能分享一下错误日志吗
标签: php jquery ajax codeigniter validation