<div class="modal-header">
<a class="modal-close-med" data-dismiss="modal" aria-hidden="true"><img src="<?php echo base_url(); ?>img/close.png"></a>
<h3 class="modal-title no-margin margin-bottom-5p-min">Change Password</h3>
</div>
<div class="modal-body">
<?php if(isset($message))echo '<span class="text-success txt-upper" style="margin-left:2rem;">'. $message .'</span>';?>
<?php echo form_open('',array('class'=>'ajaxForm')); ?>
<fieldset class="table ">
<div class="form-group">
<?php $class = form_error('newpassword')?"input-error":"" ?>
<div class="col-md-12" style="margin: 10px 0"><?php echo form_password('newpassword','','class="form-control margin-both-0 '. $class.'" id="newpassword" placeholder="New Password" autocomplete="off"'); ?><?php echo form_error('newpassword'); ?></div>
</div>
<div class="form-group">
<?php $class = form_error('conpassword')?"input-error":"" ?>
<div class="col-md-12 " style="margin: 10px 0"><?php echo form_password('conpassword','','class="form-control margin-both-0 '. $class.'" id="conpassword" placeholder="Confirm Password" autocomplete="off"'); ?><?php echo form_error('conpassword'); ?></div>
</div>
</div>
<div class="modal-footer">
<form method="post" action="" id="myform">
<?php echo form_submit('submit_btn', 'Change Password', 'class="submit btn btn-success margin-left-4p pad-1-rem margin-bottom-10"'); ?>
</form>
</fieldset>
<?php echo form_close();?>
</div>
The Controller:
public function change_password ()
{
if($this->input->post('submit_btn')){
$this->session->set_flashdata('success','Password Changed Successfully');
redirect('dashboard');
}
// Set up the form
$rules = array(
'newpassword' => array(
'field' => 'newpassword',
'label' => 'New Password',
'rules' => 'trim|required|min_length[6]|xss_clean|check_pass'
),
'conpassword' => array(
'field' => 'conpassword',
'label' => 'Confirm Password',
'rules' => 'trim|required|min_length[6]|matches[newpassword]|xss_clean|check_pass'
),
);
$this->form_validation->set_rules($rules);
$this->form_validation->set_message('required', 'this field is required');
// Process the form
if ($this->form_validation->run() == TRUE) {
$password = $this->input->post("newpassword");
$userid = $this->session->userdata("id");
if($this->user_m->change_password($password, $userid)){
$this->data['message'] = 'password changed successfully';
//$this->data['subview'] = 'home/index';
//$this->load->view('_layout_main_1', $this->data);
}else{
$this->data['message'] = 'password must have at least one uppercase letter and a number';
}
$this->data['refresh'] = true;
}
// Load view
$this->load->view('home/change_password', $this->data);
}
This currently works when i click on change password button, however i want to make it work with the enter key. This is using Codeigniter and have tried to change it using standard html input type. but it did not work. Any help would be much appreciated
Wrap around with a form and the browser will handle this. Also use a submit button type:
<?php echo form_open('',array('class'=>'ajaxForm')); ?>
<div class="modal-header">
<a class="modal-close-med" data-dismiss="modal" aria-hidden="true"><img src="<?php echo base_url(); ?>img/close.png"></a>
<h3 class="modal-title no-margin margin-bottom-5p-min">Change Password</h3>
</div>
<div class="modal-body">
<?php if(isset($message))echo '<span class="text-success txt-upper" style="margin-left:2rem;">'. $message .'</span>';?>
<fieldset class="table ">
<div class="form-group">
<?php $class = form_error('newpassword')?"input-error":"" ?>
<div class="col-md-12" style="margin: 10px 0"><?php echo form_password('newpassword','','class="form-control margin-both-0 '. $class.'" id="newpassword" placeholder="New Password" autocomplete="off"'); ?><?php echo form_error('newpassword'); ?></div>
</div>
<div class="form-group">
<?php $class = form_error('conpassword')?"input-error":"" ?>
<div class="col-md-12 " style="margin: 10px 0"><?php echo form_password('conpassword','','class="form-control margin-both-0 '. $class.'" id="conpassword" placeholder="Confirm Password" autocomplete="off"'); ?><?php echo form_error('conpassword'); ?></div>
</div>
</fieldset>
</div>
<div class="modal-footer">
<?php echo form_submit('submit_btn', 'Change Password', 'class="submit btn btn-success margin-left-4p pad-1-rem margin-bottom-10"'); ?>
</div>
<?php echo form_close();?>
If you are using this for ajax request, you can disable to send the form itself, but enter still works:
<script>
$(function() {
$('.ajaxForm').on('submit', function(e) {
// ex.: $.post(...
console.log(e);
e.preventDefault();
})
})
</script>