繁体   English   中英

如何在使用 jQuery Laravel 提交表单之前检查数据库中是否已存在记录

[英]How to check if a record already exists in a database before submitting the form using jQuery Laravel

在使用 ajax 时,我无法找出检查记录是否存在的最佳方法。如果 CNIC 存在,我想提醒“用户已存在”。 如果它不存在,我希望它将表单数据插入数据库。 我有 jquery function,它只使用 ajax 提交数据,但我想验证 CNIC,如果它存在,它会发出警报消息。 任何帮助,将不胜感激。 提前致谢。

** jquery **

$(document).ready(function() {
    $("#save1").on('click', function(e) {

        var cnic = $("#cnic").val();
         
        if (cnic == '') {
            alert("Kindly Enter CNIC");
            return false;
        }

        var gender = $("#gender").val();
        if (gender == '') {
            alert("Kindly Enter Gender");
            return false;
        }  
       
        var contactno = $("#contactno").val();
        if (contactno == '') {
            alert("Kindly Enter Contact No");
            return false;
        }

        var fname = $("#fname").val();
        if (fname == '') {
            alert("Kindly Enter Contact No");
            return false;
        }else{
            $.ajax({
                url: "/save",
                type: "get",
                data: $('#registrationform').serialize(),
                dataType: 'json',
                success: function(data) {
                    $("#patientid").val(data.patientid);
                    // console.log(data);
                }
            })
        }
        
    });
});


**

controller

**


<?php

namespace App\Http\Controllers;

use Haruncpi\LaravelIdGenerator\IdGenerator;

use Illuminate\Http\Request;
use App\Helpers\Helper;
use Illuminate\Support\Facades\DB;
use App\User;
use App\Models\patient;
use Illuminate\Support\Facades\Validator;

class Helpercontroller extends Controller
{

    function save(Request $request)
    {
        $temppatientId = IdGenerator::generate(['table' => 'patients', 'length' => 5, 'prefix' => 
        '22']);
        $patientid = $temppatientId + 1;
        $query = new patient;
        $query->patientid = $patientid;
        $query->fname = $fname;
        $query->lname = $lname;
        $query->cnic = $cnic;
        $query->contactno = $contactno;
        $query->gender = $gender;
        $query->age = $age;
        $query->dob = $dob;
        $query->city = $city;
        $query->address = $address;
        $query->husbandname = $husbandname;
        $query->fathername = $fathername;
        $query->bloodgroup = $bloodgroup;
        $query->maritalstatus = $maritalstatus;
        

        $query->save();
        return $query;
    }


快速的方法是使用 Laravel 的验证。

public function save(Request $request)
{
   $validator = \Validator::make($request->all(), [
        // I suppose your table name is 'patients'
        'cnic' => 'required|unique:patients',
    ]);
    
    if ($validator->fails()) {
        // you can return a custom message if needed
        return response()->json(['success' => false, 'errors' => $validator->errors()->all()]);
    }

    // ... your actuel code to save data

    return response()->json(['success' => true, 'patient' => $query]);
}

在您的 Ajax 电话中:

$.ajax({
      url: "/save",
      type: "get",
      data: $('#registrationform').serialize(),
      dataType: 'json',
      success: function(data) {
          if (data.success === false) {
             alert('User already exist !');
          } else {
             $("#patientid").val(data.patient.patientid);
             // console.log(data.patient);
          }
      }
})

尊重变量的大小写,不要使用 GET 方法来发布资源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM