Я пытаюсь включить AJAX в свою работу. Я хочу, чтобы он регистрировал пользователя, как только была нажата кнопка входа в систему. Ниже приведен соответствующий код. Вид вызывает функцию входа в мой контроллер.Вход AJAX и codeigniter
Посмотреть
<form id="loginform" class="navbar-form navbar-right" action="https://siteurl/CI/index.php/postedLinks/login" method="post" role="search">
<div class="form-group">
<input type="text" name="username" class="form-control" placeholder="Username">
<input type="password" name="password" class="form-control" placeholder="Password">
</div>
<button type="submit" class="btn btn-default">Login</button>
</form>
Контроллер
public function login(){
$this->form_validation->set_rules('username','Username', 'trim|required');
$this->form_validation->set_rules('password','Password', 'trim|required|callback_check_login');
if ($this->form_validation->run()==false) {
$data['links'] = $this->links_model->get_links();
$this->load->view('postedLinks', $data);
}else{
redirect(site_url('postedLinks'), 'refresh');
}}
public function check_login($password){
$username = $this->input->post('username');
$result = $this->links_model->login($username,$password);
if ($result) {
$sess_array = array();
foreach ($result as $row) {
$sess_array = $arrayName = array('id' => $row->id, 'username' => $row->username);
$this->session->set_userdata('logged_in', $sess_array);
}
return true;
} else{
$this->form_validation->set_message('check_login', 'Invalid Username or Password');
return false;
}
}
Это прекрасно работает, однако мне нужно добавить Аякса в него, однако, когда я пытаюсь это никогда не работает. Любая помощь приветствуется. Ниже я попытался разместить на мой взгляд.
$(document).ready(function() {
// process the form
$('#loginform').submit(function(event) {
// get the form data
// there are many ways to get this data using jQuery (you can use the class or id also)
// process the form
$.ajax({
url : "https://siteurl/CI/index.php/postedLinks/login"
type : "POST",
dataType : "json",
data : {"username" : username, "password" : password},
success : function(data) {
// do something
},
error : function(data) {
// do something
}
});
// stop the form from submitting the normal way and refreshing the page
event.preventDefault();
});
Итак, вы хотите вставить данные о своем пользователе при нажатии кнопки отправки справа? –
Я понял это так, как он хотел бы, чтобы форма входа была выполнена через AJAX вместо обычного запроса POST и последующей смены страницы. ??? – Juned
Я отредактировал эту ссылку, также да, что я хочу сделать – ZeeSoft