Welcome Guest, Not a member yet? Register   Sign In
Error "POST" 500 with ajax request.
#1

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

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#2

(This post was last modified: 09-25-2018, 01:43 PM by ignitedcms.)

What does the <form> look like? Additionally, as an aside you want to hash your passwords, not sure if you models handle that though.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#3

(This post was last modified: 09-26-2018, 06:46 AM by HarrysR.)

(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

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#4

Am I assuming you are loading your models in the config file?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#5

(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..

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#6

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.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#7

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.
Reply
#8

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

Yeah but it is a modal so I figured you need ajax.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#10

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!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB