-2

I am trying to submit a form using JQuery but it does not seem to be submitting. What am I doing wrong? This is my JS code: It does not log the hello word.

document.querySelector("form#myForm").addEventListener('submit', validateForm)
    function validateForm(e)
    {
        e.preventDefault();
        var values = $('#myForm').serialize();

         $.ajax({
            url: "<?php echo route('validate') ?>",
            method: 'GET',
            async:false,
            data: values,
            success: function(data) {
                var data = JSON.parse(data);
                var text = data.text;
                var image_url = data.image;
                swal({
                    html: "<div style='text-align: left; margin-left: 10px'><strong>Preview</strong></div>",
                    showCancelButton: true,
                    confirmButtonText: "Yes",
                    cancelButtonText: "No",
                    confirmButtonColor: "#00ff55",
                    cancelButtonColor: "#999999",
                    reverseButtons: true,
                }).then((result) => {
                    if (result.value) {
                        $("#myForm").submit(function(e){
                            console.log('hello')
                        });
                    }
                });
            }
    });
    return false;
}

This is the form opening tag attributes - <form action="{{url('test')}}" method="post" onsubmit="return validateForm(e);" id="myForm" name="a-form">

shekwo
  • 1,411
  • 1
  • 20
  • 50
  • 1- First check the console and network tab in the browser. 2- If no error in console and network tab gives the 200 status, then **dd** in validate controller to make sure if form data shows – Mukkaram Waheed Aug 11 '20 at 06:16
  • use like this `url: "{{route('validate')}}"` – A.A Noman Aug 11 '20 at 06:17
  • @Adam I already `dd` in the controller but nothing still. – shekwo Aug 11 '20 at 23:01
  • This solved my problem - https://stackoverflow.com/questions/3303874/jquery-form-not-submitting-with-id-submit-but-will-submit-with-a-subm – shekwo Aug 12 '20 at 01:39

1 Answers1

-1

Since you don't see your hello on your console (developer tools), did you also check for any errors. It's most likely not printed due to that.

As for your code, document.querySelector("form#myForm").addEventListener('submit', validateForm) is basically doing the same thing as onsubmit="return validateForm(e);". So it'd actually be doing the request twice. You can remove either one. Also do make sure your route is printed correctly on the url.

sykez
  • 2,015
  • 15
  • 14