Support the ongoing development of Laravel.io →
Requests Input Database

I would like to make my own user registration using Ajax. I created form, on submit I call function registerUser

$('#registration-form').submit(registerUser);

  function registerUser() {
      var $form = $(this);

      $form.find('.error').html('');
      $form.find('input[type=text],input[type=password]').removeClass('error');

      var values = {};
      $.each($form.serializeArray(), function (i, field) {
          values[field.name] = field.value.trim();
      });

      var errors = {};
      //checkRegistrationData(values, errors);
      
      if(!jQuery.isEmptyObject(errors)) {
        $.each( errors, function( field, error ) {
          if(errors[field]) {
            $form.find('.error.'+field).html(error);
            var id = 'registration-' + field.replace('error-', '');
            $form.find('#' + id).addClass('error');
          }
        });
      } else {
      var options = {
            url: $form.attr("action"),
            type: $form.attr("method"),
            data: $form.serialize(),
            dataType: 'JSON'
        };

      $.ajax(options).done(function (data) {
        console.log(data);
      });  
    }

      return false;
  }

also I added route

Route::group(['middleware' => ['web']], function () {
    Route::post('register', 'UserController@register');
});

and added method in User controller

class UserController extends Controller
{
    public function register() {

    	if(Request::ajax()) {
	      
	    }     
    }
}

but I got an error

POST http://localhost:8080/snimu/public/register 500 (Internal Server Error)

What's wrong? And How to get data in controller to save it to database?

I added meta tag to master page

<meta name="csrf-token" content="{{ csrf_token() }}">

and at the begining js file added

$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
});

but error stays the same

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.