-1

I have this form

MyForm.html

<form id="MyForm" ENCTYPE="application/x-www-form-urlencoded" class="form form-horizontal" autocomplete="off">
  <fieldset>

    <!-- .control-group -->

    <div class="control-group">
      <div class="control-label align-right">
        <label for="frm-shareDeliveryPopupForm-shareDeliveryForm-senderEmail">Your e-mail</label>
      </div>
      <!-- .control-label -->
      <div class="control-field">
        <input type="email" name="Email" id="senderEmail" required class="input">
      </div>
      <!-- .control-field -->
    </div>
    <!-- .control-group -->
    <div class="control-group" id="PasswordID">
      <div class="control-label align-right">
        <label for="senderPassword">Your password</label>
      </div>
      <!-- .control-label -->
      <div class="control-field">
        <input type="Password" name="Pass" id="Password" class="input" pattern=".{4,}" title="6 characters minimum" required>
      </div>
      <!-- .control-field -->
    </div>
    <!-- .control-group -->


    <!-- .control-group -->



    <div id="SendDiv" class="control-group">

      <div class="control-label"></div>
      <!-- .control-label -->
      <div class="Centered" id="sendButton">
        <a href="#" data-name="download-button" class="btn btn-primary btn-large uppercase">
          <span>Download</span>
          <span class="spinner"></span>
        </a>
      </div>
      <!-- .control-field -->
    </div>
    <!-- .control-group -->

  </fieldset>

</form>

And here is my JS file for processing:



/******************** START Submit Form by the click of an element ID *****************************************/
 $(document).ready(function(){
   $("#SendDiv").click(function(){
     $("#MyForm").submit();
   });
})
/******************** END Submit Form by the click of an element ID *****************************************/

/************** FORM PROCESSOR *******************************/


// process the form
    $("#MyForm").submit(function(event) {
        event.preventDefault();
        // get the form data
        // there are many ways to get this data using jQuery (you can use the class or id also)
        var formData = {
            'Email'                 : $('input[name=Email]').val(),
            'Password'              : $('input[name=Pass]').val()
        };

        // process the form
        $.ajax({
            type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
            url         : './process.php', // the url where we want to POST
            data        : formData, // our data object
            dataType    : 'json', // what type of data do we expect back from the server
            beforeSend: function()
            {
                
                $("#error").fadeOut();
            },
            encode      : true
        })
            // using the done promise callback
            .done(function(data) {

                // log data to the console so we can see
                console.log(data); 

                // here we will handle errors and validation messages
                if (!data.success) {
                    
    // handle errors  ---------------
    if (data.errors.Error) {
        window.location.href= "./error.php";
    }
    
    } else { 

                // ALL GOOD! just show the success message!
                    
                    window.location.href= "./success.php";
                }
            })

            // using the fail promise callback
            .fail(function(data) {

                // show any errors
                // best to remove for production
                console.log(data);
            });

        
    });


I tried to send the form when the DIV with element ID "SendDiv" is clicked. However, the form refuse to post after multiple tries.

I have tried to submit form by element ID or element class and none seem to work. I do not want to use the regular submit button as I do not want a page refresh during form processing. Can anyone help with this?

6
  • Are there any errors in the console? If you check the Network tab, do you see the AJAX request being sent?
    – Barmar
    Commented Jun 16 at 23:29
  • Why isn't the FORM PROCESSOR code inside $(document).ready()?
    – Barmar
    Commented Jun 16 at 23:30
  • FYI - you cant send emails using JQuery. You are sending an AJAX request to the process.php file using jQuery, which sends the email in PHP.
    – Dula
    Commented Jun 16 at 23:33
  • @Barmar I have checked the console too many times and I do not see any errors being pulled or the processor file being called. It's just still. Nothing works when I do click the 'SendDiv' element in form which ordinarily should submit the form. Also, I didn't put the form processor in the '$(document).ready()' section because I have been using the code this way in other forms and they work. This is strange to me. I really need help.
    – Jared
    Commented Jun 17 at 5:12
  • Is the form processor at the end of the body? It needs to be after MyForm so the selector will be able to add the event listener. This is why code that addss event listeners is normally put inside the ready function.
    – Barmar
    Commented Jun 17 at 14:46

1 Answer 1

0

Put the form processing inside $(document).ready(), like other code that creates event listeners. Otherwise, it may run before MyForm is in the DOM, and no event listener will be added.

$(document).ready(function() {
  $("#SendDiv").click(function() {
    $("#MyForm").submit();
  });

  // process the form
  $("#MyForm").submit(function(event) {
    event.preventDefault();
    // get the form data
    // there are many ways to get this data using jQuery (you can use the class or id also)
    var formData = {
      'Email': $('input[name=Email]').val(),
      'Password': $('input[name=Pass]').val()
    };

    // process the form
    $.ajax({
        type: 'POST', // define the type of HTTP verb we want to use (POST for our form)
        url: './process.php', // the url where we want to POST
        data: formData, // our data object
        dataType: 'json', // what type of data do we expect back from the server
        beforeSend: function() {

          $("#error").fadeOut();
        },
        encode: true
      })
      // using the done promise callback
      .done(function(data) {
        // log data to the console so we can see
        console.log(data);

        // here we will handle errors and validation messages
        if (!data.success) {
          // handle errors  ---------------
          if (data.errors.Error) {
            window.location.href = "./error.php";
          }
        } else {
          // ALL GOOD! just show the success message!
          window.location.href = "./success.php";
        }
      })
      // using the fail promise callback
      .fail(function(data) {

        // show any errors
        // best to remove for production
        console.log(data);
      });
  });
})

1
  • Barmar, For some reasons, I am going to accept your answer. I have tried the snippet you sent on a separate form and it worked as specified. However, when I put my other parts of the html in the page that form is, it fails to work. Something is wrong with my files and I do not know what that is at the moment. Have replicated this over and over again and it doesn't work. Only works if it's just this plan code without me adding rest of my html codes. Thanks.
    – Jared
    Commented Jun 17 at 19:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.