Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
¿Cómo devolver el resultado usando AJAX en CodeIgniter?

Quiero preguntar cómo devolver la respuesta como return 'Password successfully updated'; o return 'Failed to Update Record'; en mi vista usando el AJAX. Funciona bien antes sin AJAX después de enviarlo y devolver la respuesta.

Ahora solo incluyo el AJAX en mi formulario, el formulario funciona bien. pero el mensaje de error y éxito (respuesta) no aparece después de enviarlo.

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); }); }); });

Modelo

 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!'; } }

Controlador

 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(); } }

Vista

 <?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 answers
Answer question

0

En el modelo solo echo en lugar de 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!