Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

210
Visualizações
How to return the result using AJAX in CodeIgniter?

I want to ask how to return the response like return 'Password successfully updated'; or return 'Failed to Update Record'; on my view using the AJAX. It's working fine before without AJAX after submitting it returning the response.

Now I just include the AJAX on my form the form is working fine. but the error and success message (response) not showing up after submitting.

Ajax

 $(document).ready(function(){
    $('form.jsForm').on('submit', function(e){
        e.preventDefault();
        $.post('change_password_process', $('form.jsForm').serialize(), function(data){
            $('div.response').html(data);
        });
    });
});

Model

public function updatePassword($data){ //update user password
    $oldPassword = $data['oldPassword'];
    $user = $data['user'];

    $this->db->select('password')
             ->from('users')
             ->where('username', $user);
    $query = $this->db->get();
    $row = $query->row();

     if($this->bcrypt->check_password($oldPassword, $row->password)){
        if($this->db->set('password', $data['newPassword'])
                        ->where('username', $user)
                        ->update('users')){
            return 'Password successfully updated';
        } else {
            return 'Failed to Update Record';
        }

    } else {
        return 'Old password is incorrect!';
    }
}

Controller

if($this->form_validation->run() == TRUE){

        $data = array(
                'user' => $this->input->post('username'),
                'oldPassword' => $this->input->post('oldPassword'),
                'newPassword' => $this->bcrypt->hash_password($this->input->post('newPassword')),
                'cPassword' => $this->input->post('cPassword')
        );

        //this is the response from MODEL 

     $this->session->set_flashdata('response',$this->user_accounts_model->updatePassword($data)); 
        //redirect('user_accounts/change_password_form');

    } else {
        $this->change_password_form();
    }
}

View

<?php if($response = $this->session->flashdata('response')) :?>

    <div class="alert alert-dismissible alert-success">
      <?=$response?>
    </div>
<?php endif; ?> //this is the form result w/out ajax

<div class="response"></div> //this is using ajax
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In model just echo instead of return.

if($this->db->set('password', $data['newPassword'])
                        ->where('username', $user)
                        ->update('users')){
            echo 'Password successfully updated';
        } else {
            echo 'Failed to Update Record';
        }

    } else {
        echo 'Old password is incorrect!';
    }
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda