【发布时间】:2017-11-22 15:15:26
【问题描述】:
当我提交表单错误显示完美但当我再次提交数据输入数据库但其他条件不起作用我想隐藏表单并显示成功消息
如果密码长度小于6,则第一次响应
密码必须大于6
第二次响应为空,否则条件应该可以工作,但如果数据输入成功,我不想隐藏表单
<div class="modal-body">
<div id="successResults"></div>
<form method="" action="" class="form-horizontal signup-hide" role="form" id="signup-form">
<div id="ajaxResults"></div>
<input type="text" class="model-signup-fields"
name="first_name" value="<?php echo $this->input->post('first_name');?>">
<input type="email" class="model-signup-fields" name="email" value="<?php echo $this->input->post('email');?>">
<input type="password" class="model-signup-fields" name="password" value="<?php echo $this->input->post('password');?>">
<input type="password" class="model-signup-fields" name="confirm_password" value="">
<br>
<input type="submit" name="remove" id="this_signup" class="claim btn btn-info model-signup " onclick="" value="signup" />
</form>
</div>
控制器
public function register() {
$data['title'] = 'Register';
$this->load->model('auth_model');
if (count($_POST)) {
$this->load->helper('security');
$this->form_validation->set_rules('first_name', 'First name', 'trim|required');
// $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|min_length[6]|matches[password]');
if ($this->form_validation->run() == false) {
// $data['notif']['message'] = validation_errors();
// $data['notif']['type'] = 'danger';
$status = validation_errors();
if ( $this->input->is_ajax_request() ) {
echo json_encode($status);
}
}
else {
// $data['notif'] = $this->auth_model->register();
$success_reg = $this->auth_model->register();
}
}
if ($this->session->userdata('logged_in')) {
redirect(base_url('dashboard'));
exit;
}
$this->load->view('includes/header', $data);
$this->load->view('home/index');
$this->load->view('includes/footer');
}
脚本
$(function()
{
$('.model-signup').on('click', function(e){
e.preventDefault();
var form = $("#signup-form").serialize();
$("#this_signup").css({"background-image":"url(<?PHP echo
base_url(); ?
>assets/images/loading1.gif)",
"background-repeat": "no-repeat",
"background-position": "10% 50%"});
$.ajax({
url:"<?PHP echo base_url(); ?>auth/register",
type:"POST",
dataType: 'json',
cache: false,
data:form,
success: function(response) {
alert(response);
var result2 = response;
console.log(result2);
if(result2)
{
alert("if");
$('#ajaxResults').addClass('alert alert-danger').html(result2);
$("#this_signup").css({"background-image":"",
"background-repeat": "",
"background-position": ""});
}
if(response == ''){
alert("else");
$("#this_signup").css({"background-image":"",
"background-repeat": "",
"background-position": ""});
$(".signup-hide").hide();
$('#successResults').addClass('alert alert-success').html('You successfully register now you can login');
}
response='';
}
});
});
});
【问题讨论】:
-
请澄清-您的问题不清楚。请用具体的步骤/条件对其进行分解,以便我们可以跟踪您所说的是/没有按应有的方式发生。
-
我第一次提交表单错误显示此字段是必需的等,但第二次当数据输入数据库时,其他条件在脚本中工作但它无法工作
-
为什么不行?如果数据输入正确,则 else 中的脚本应该执行
-
这就是我的观点,为什么其他条件不起作用
标签: jquery ajax codeigniter