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.