Hello, I am trying to do the processing of the form using jquery. The project is done on laravel 5.3. The script should check the form for the number of characters and, if there are less than 3, notify the user about it and stop the request without adding anything to the database. Plus when sending data page should not reload. The code is: newcategory.blade.php

@extends('layouts.app') @section('content') <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .center { text-align: center; } </style> <!--<script src="public/js/jquery-3.1.1.min.js"></script>--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script> $.(document).ready(function () { $("#send").click(function () { $('#messageShow').hide(); var title = $("#title").val(); var fail=""; if (title.length < 3) fail="НазваниС ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠΎΡΡ‚ΠΎΡΡ‚ΡŒ ΠΈΠ· 3-Ρ… ΠΈ Π±ΠΎΠ»Π΅Π΅ символов"; if (fail!=""){ $('#messageShow').html(fail + "<div class='clear'><br></div>") $('#messageShow').show(); return false; } $.ajax({ url: '/newcategory', type:'POST', data:{'title': title}, cache:false, success: function (response) { var messageResp = new Array('ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½','ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ Π½Π΅ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½,ошибка','Π—Π°ΠΏΠΎΠ»Π½ΠΈΡ‚Π΅ Ρ„ΠΎΡ€ΠΌΡƒ'); var resultStat = messageResp[Number(response)]; if(response == 0){ $("#title").val(""); } $("#messageShow").text(resultStat).show().delay(1500).fadeOut(800); } }) }) }) </script> </head> <body> <div class="center"> <form method="POST" enctype="multipart/form-data"> {{ csrf_field() }} <input type="text" name="title" id="title" placeholder="Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΡŽ"> <input type="submit" class="btn-primary" value="Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ" id="send"> <div id="messageShow"></div> </form> </div> </body> </html> @endsection 

CategoriesController.php

 <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Categories; use Illuminate\Support\Facades\Input; use Validator; class CategoriesController extends Controller { public function store(){ $input = Input::all(); $rules = array( 'title' => 'required', ); $validator = Validator::make($input,$rules); if($validator->fails()) { return $validator->errors(); } if($validator->passes()) { $newcategory = new Categories; $newcategory->title=Input::get('title'); $newcategory->save(); return view('post.newcategory'); } } public function index(){ return view('post.newcategory'); } } 

routes / web.php

 Route::get('newcategory', 'CategoriesController@index'); Route::post('newcategory','CategoriesController@store'); 

When using this code, the form data is freely sent and stored in the database, even if the entered characters in the form are less than 3. Can you please tell me what I'm doing wrong? Thanks in advance.

  • Are there any bugs in the browser console? - Alexey Ukolov
  • Hello. Yes, when you send data, "Uncaught SyntaxError: Unexpected token" appears in the row $. (Document) .ready (function () { - Dust0

2 answers 2

As for the error, you have an extra dot at the beginning after the $ sign. This sign means the jQuery function to which we pass arguments, in this case document. In addition, to stop the form being submitted before validation, you need to stop the default browser action using event.preventDefault ().

Your code should start like this.

  $(document).ready(function () { $("#send").click(function (ev) { ev.preventDefault(); var title = $("#title").val(); var fail=""; ... 

and it would be better to hang your function on the submit event of the form itself, which can occur just by pressing Enter if the user is in a form. Add Id to the form and handle the send event on it

  $(document).ready(function () { $("#sendForm").submit(function (ev) { ev.preventDefault(); var title = $("#title").val(); var fail=""; ... 
  • Thank you for your help, now everything has worked! - Dust0

In order to be able to send the form you need csrf-token. You have it in the form but you do not send it. Assign an event id="send-form" event write to ('#send-form').submit() , and send data to ajax via data: ('#send-form').serialize() to send all fields from the form immediately.

  • Thank you for your help, now everything has worked! - Dust0