rkmourya
rkmourya

Reputation: 466

jquery validate preventing submit being passed in post

I am using jquery validate to check the existing phone number in my database. I am using jquery validate remote attribute for this.

remote: {
    url: "ajax-checkmobile.php",
    type: "post",
    data: {
      cust_id: function() {
        return $("#cust_id").val();
      }
    }
  }

However this is preventing the submit value being passed on form submission via POST.

<input name="Submitcustdata" type="submit" class="chromeButton" id="Submitcustdata" value="Edit Customer" /></td>

After removing the above code submit value gets passed correctly. Any idea what could be preventing value from being passed

Upvotes: 4

Views: 193

Answers (2)

J. Bruni
J. Bruni

Reputation: 20492

I have developed the following code, which I use in the context of submiting forms using AJAX. This is not exactly your case, but the same code will make your <input type="submit"> value to be submitted:

function submit_click() {
    var $input = $(this);
    var name = $input.attr('name');
    if (typeof name == 'undefined') return;
    var value  = $input.attr('value');
    var $form  = $input.closest('form');
    var $hidden = $('<input type="hidden" />').attr('name', name).attr('value', value);
    $form.find('input[type=hidden][name="' + name + '"]').remove();
    $form.append($hidden);
};

$(document).on('click', 'form.ajax input[type=submit]', submit_click);

I use the class="ajax" in the form element to activate this code. You can change according to your needs.

What it does? It creates a hidden form input with the same name and value of the clicked submit input. So, when the post happens programatically later, the value is properly submitted.

Ah, you asked what could be preventing value from being passed... the validation plugin catches the form submission, and performs its stuff. After validating, it triggers the form submission. When this happens, it is not the <input> element click which is trigerring the form submission anymore. It is the JavaScript code of the plugin. So, the "<input>" click event is lost. This is why it is not submitted. (Makes sense? If this explanation is not well written, let me know.)

Upvotes: 1

Kristian
Kristian

Reputation: 21830

validate stops the post from happening from occurring in order to validate. that is by design.

so, if its not submitting, then its thinking that the field is invalid, i'm guessing.

Upvotes: 0

Related Questions