1

I am new in laravel and need help.

These are the values I'm getting via ajax, all values are to be stored in db, but how do I validate[check] if there are existing values or not before submitting a form using ajax?

$("#submit").click(function(e){  
        e.preventDefault();
        var url = "{!! URL::to('/') !!}";
        var id="{!! @$department->id !!}";
        var token = "{!! csrf_token() !!}";
        var customer_id = $("#customer_id").val();
        var visiting_address1 = $("#visiting_address1").val();
            var department_id = $("#department_id").val();  

        // AJAX Code To Submit Form.
        $.ajax({
            type: "POST",
            url: url+"/customer/"+customer_id+"/popup_store",  
            data: { '_token':token, 'rest':'true', 'customer_id':$("#customer_id").val(), 'main_address':$("#main_address").val(),'visiting_city':$("#visiting_city").val(),'visiting_address':$("#visiting_address1").val(),'visiting_zip':$("#visiting_zip").val()},
            async : false,
            success : function(data) { 
            if(data == "success")
            {
                $("#addrecords").modal('hide');

            }
            else 
                alert(data);
            }
        });

    }); 
8
  • Please try to be more clear with your question in future, it was very confusing to read. Are you trying to validate form data, or verify whether there are duplicates? Commented Apr 2, 2016 at 10:37
  • Hi Tim Ogilvy avoid duplicates values Commented Apr 2, 2016 at 10:54
  • only unique values Commented Apr 2, 2016 at 10:55
  • So you only want to add a new popup store if there isn't one already with the same name? Commented Apr 2, 2016 at 11:00
  • Have a bit of a look at stackoverflow.com/questions/33139076/… and stackoverflow.com/questions/18839941/…, maybe there will be some ideas. Commented Apr 2, 2016 at 11:10

1 Answer 1

0

First of all define you Jquery validation for you form like this:

$("#myForm").validate({
    // Specify the validation rules
    rules: {
        name: "required",
    },
    // Specify the validation error messages
    messages: {
        name: "Please enter name",
    },
    submitHandler: function (form) {
        // leave it blank here.
    }
});

And then in your click button of submit button, write if condition to check validations:

$("#submit").click(function (e) {
    if ($('#myForm').valid()) {
        // your Ajax Call Code
    } else {
        return false;
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Ali i have try your idea but not working properly
Ali actually two form page one is order form page another one is department from page, here in my order form page la apply modal popup plus button are you understand Ali
then when i press plus button open modal window . that values are getting from Ajax see abowe my ajax code
Can't get u actually.
what i am expecting that record exist or not, if existing record means show message or alert
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.