CodeIgniter Forums
Error "POST" 500 with ajax request. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Error "POST" 500 with ajax request. (/showthread.php?tid=71797)



Error "POST" 500 with ajax request. - HarrysR - 09-25-2018

I've created a modal register form and when i submit the form i get the following error:
Code:
jquery-3.3.1.min.js:2
POST http://localhost/[website_name]/users/create_user 500 (Internal Server Error)


I've checked the controller and model and i cant find any errors.
Any ideas?!

Controller
Code:
 public function create_user(){
   $email = trim( $this->input->post('email') );
   $email_exist = $this->user_model->check_email_exist($email);

   if ($email_exist) {
     $result['status'] = 'failed';
     $result['message'] = $this->lang->line('error_email_exist');
   } else {
     //Pass enc
     $enc_password = md5($this->input->post('passInput'));
     $register = $this->user_model->register($enc_password);
     if ($register) {
       $result['status'] = 'success';
     } else {
       $result['status'] = 'failed';
       $result['message'] = 'Please fill all the fields';
     }
   }
   return json_encode($result);
 }

Model
Code:
 public function register($enc_password){
   $user_name = convert_accented_characters($this->input->post('screenNameInput'));
   $user_name_slug = url_title($user_name);

   $data = array();

   $data['user_group_id'] = $this->input->post('userGroupInput');
   $data['screen_name'] = $this->input->post('screenNameInput');
   $data['username'] = $this->input->post('usernameInput');
   $data['email'] = $this->input->post('email');
   $data['password'] = $enc_password;
   $data['slug'] = $user_name_slug;
   $data['register_status'] = 1;
   $data['unique_id'] = random_string('numeric', 6);

   if ( $this->input->post('userGroupInput') == 1 && $this->input->post('websiteInput') ) {
     $data['website'] = $this->input->post('websiteInput');
   }

   $this->db->insert('users', $data);
   $user_id = $this->db->insert_id();

   $contact_data = array(
     'primary_num' => $this->input->post('phoneInput'),
     'info_group_name' => $this->input->post('screenNameInput'),
     'city' => $this->input->post('cityInput'),
     'to_user_id' => $user_id
   );

   $registered = $this->db->insert('contact_info', $contact_data);

   if ($registered) {
     return true;
   } else {
     return false;
   }
 }

Ajax function:
Code:
   $('#userRegister').on('submit', function(e){
     e.preventDefault();
     var form = $(this);

     $.ajax({
       url: form.attr('action'),
       type: "POST",
       data: form.serialize(),
       dataType: "json",
       success: function(data){
         if(data.status == 'success') {
           window.location.href = 'users';
         } else if (data.status == 'failed' ) {
           $('.error-messages').html(data.message);
           $('.error-messages').fadeIn('fast');

           setTimeout(function(){
             $('.error-messages').fadeOut('fast');
           }, 2000);
         }
       }
     });
   });



RE: Error "POST" 500 with ajax request. - ignitedcms - 09-25-2018

What does the <form> look like? Additionally, as an aside you want to hash your passwords, not sure if you models handle that though.


RE: Error "POST" 500 with ajax request. - HarrysR - 09-25-2018

(09-25-2018, 01:40 PM)ignitedcms Wrote: What does the <form> look like?

Yes sorry my mistake..
Here...

Code:
<?php echo form_open('users/create_user', 'class="customForm" id="userRegister"'); ?>
 <div class="row">
   <div class="col-lg">
     <div class="form-group">
       <select name="userGroupInput" id="userGroupInput" class="form-control" required>
         <option value="">-- Account type --</option>
         <option value="1">Group</option>
         <option value="2">Individual</option>
       </select>
     </div>
   </div>
   <div class="col-lg mx-0">
     <div class="form-group">
       <input type="text" class="form-control" name="cityInput" id="cityInput" placeholder="City" required>
     </div>
   </div>
 </div>
 <div class="row moreInfo hidden">
   <div class="col-lg">
     <div class="form-group">
       <input type="text" class="form-control" name="websiteInput" id="websiteInput" placeholder="Website">
     </div>
   </div>
 </div>

<div class="row">
    <div class="col-lg">
      <div class="form-group">
        <input type="password" class="form-control" name="passInput" minlength="4" id="passInput" placeholder="Password" required>
      </div>
    </div>
    <div class="col-lg">
      <div class="form-group">
        <input type="password" class="form-control" name="passInput2" minlength="4" id="passInput2" equalTo="#passInput" placeholder="Re-enter password" required>
      </div>
    </div>
  </div>

 <div class="row">
   <div class="col-md">
     <div class="form-group">
       <input type="email" class="form-control" name="email" id="email" placeholder="Email" required>
       <small class="text-danger text-center"><span id="email_result"></span></small>
     </div>
   </div>
   <div class="col-md">
<div class="form-group">
<input type="text" class="form-control" name="usernameInput" id="usernameInput" placeholder="Username" required>
</div[/font][/size]
   </div>
 </div>
 <div class="row">
   <div class="col-lg-2 mt-3">
     <button type="reset" form="userRegister" class="btn btn-danger btn-block"><i class="fas fa-broom"></i></button>
   </div>
   <div class="col-lg mt-3">
     <button type="submit" id="register_btn" form="userRegister" class="btn btn-custom btn-block "><i class="fas fa-pencil-alt"></i> Register</button>
   </div>
 </div>
</form>

I've included some of the inputs.. 


I've removed


RE: Error "POST" 500 with ajax request. - ignitedcms - 09-25-2018

Am I assuming you are loading your models in the config file?


RE: Error "POST" 500 with ajax request. - HarrysR - 09-26-2018

(09-25-2018, 09:26 PM)ignitedcms Wrote: Am I assuming you are loading your models in the config file?

You mean the autoload file..


RE: Error "POST" 500 with ajax request. - ignitedcms - 09-26-2018

Yes, my guess is something is failing in the controller, strip it down to the bare minimum to make sure it is getting there and passing something back, so remove any model calls.


RE: Error "POST" 500 with ajax request. - kilishan - 09-26-2018

You need to find your server's error logs and see what that's saying. 500 errors are notoriously difficult to track down unless you can look at the log file itself. The log should have the exact error you're running into, though.


RE: Error "POST" 500 with ajax request. - InsiteFX - 09-26-2018

This is why they tell you to get it working with pure html and php first,
then add the JavaScript Ajax when it is already working.


RE: Error "POST" 500 with ajax request. - ignitedcms - 09-26-2018

Yeah but it is a modal so I figured you need ajax.


RE: Error "POST" 500 with ajax request. - HarrysR - 09-28-2018

Well guys the thing was that i was trying to pass a value that was not inserted as a value in inputs. In other words, I was trying to add "username" to the table, while there was not any inputs for username.. I removed it from model and now it works like a charm!