Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to submit disabled inputs as part of a jQuery Ajax request? [duplicate]

Tags:

jquery

I have a pretty complex form that provides real-time validation and feedback to the user using it as they type in data. Parts of the form may be disabled due to form "business logic" of the inputs, but the server side needs to know the value of these disabled inputs to do some of its checking in the Ajax requests.

I was using jQuery to serialize the entire form, $(form).serialize(), and sending all of this data to the server during my Ajax requests, but this serialized data will be missing any inputs that are disabled. So what is the best way to include all of the form data (even disabled inputs) with each Ajax request?

My current thought is to attach a function to ajaxStart (callback) that would create a client side hash-map of all form inputs' disabled attribute, enable all the inputs, serialize, and then set each form's disabled attribute back to its original value. But this seems overly complex, and I wanted something simpler that wouldn't be as brittle.

like image 351
MikeN Avatar asked May 11 '09 19:05

MikeN


People also ask

Can I make AJAX requests by jQuery?

jQuery ajax() Method. The jQuery ajax() method provides core functionality of Ajax in jQuery. It sends asynchronous HTTP requests to the server.

What is the use of AJAX () method?

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

What type of request does AJAX send to the server by default?

contentType: It's default value is: “application/x-www-form-urlencoded” and it is used when data send to the server.

Which object can be used to retrieve data in AJAX?

XMLHTTPRequest object is an API which is used for fetching data from the server. XMLHTTPRequest is basically used in Ajax programming. It retrieve any type of data such as json, xml, text etc. It request for data in background and update the page without reloading page on client side.


5 Answers

Try doing something like this to serialize the form:

   $(function() {
      $('input[type=button]').click( function() {
          var form = $('form').serialize();
          $('input[disabled]').each( function() {
              form = form + '&' + $(this).attr('name') + '=' + $(this).val();
          });

      });
   });

Test HTML

<form action="/action">
   <input type='text' disabled='disabled' value='hey' id='hey' name='hey' />
   <input type='text' value='ho' id='ho' name='ho' />
   <input type='text' value="hi" id='hi' name='hi' />
   <input type='button' value="Serialize" />
</form>

Output

ho=ho&hi=hi&hey=hey
like image 129
tvanfosson Avatar answered Oct 13 '22 20:10

tvanfosson


Parts of the form may be disabled due to form "business logic" of the inputs

You can use "readonly" attribute to disable those input fields. Do like this:

<input type="text" readOnly="readOnly" />

The readonly attribute specifies that an input field should be read-only. A read-only field cannot be modified. However, a user can tab to it, highlight it, and copy the text from it.

from: HTML input readonly Attribute

A read-only field also can be submitted to server as query string.

like image 31
Yang Juven Avatar answered Oct 13 '22 22:10

Yang Juven


If your server accepts JSON, you can create a JSON object with jQuery (use jQuery select statements to include the necessary input tags), then send it to the server as an object - more information here: http://encosia.com/2009/04/07/using-complex-types-to-make-calling-services-less-complex/.

Basically, do something like this:

var formInputs = { };

$('input[type=text]').each(function() {
  formInputs[this.id] = this.value;
});
like image 33
Gabriel Florit Avatar answered Oct 13 '22 20:10

Gabriel Florit


Well another option is to simply add a hidden field with the value you wan't posted to the server.

Remember to use a different (dummy) id for the tag that you disable to avoid any conflict.

like image 34
Michael Ringholm Sundgaard Avatar answered Oct 13 '22 20:10

Michael Ringholm Sundgaard


To send the disabled value to the backend using .serilize() through a form submit using jQuery to Ajax, it is:

<HTML>
    <HEAD>
        <script>
            $(document).ready(function(){
                $("#submid").click(function() { // loginForm is submitted
                    $('#ayyappa').removeAttr('disabled');
                    $.ajax({
                        url: "urls",
                        data:{
                            "sel_opt": $("#ayyappa").attr("value"),
                        }

                    }); // Ajax
                    $("#Number").attr("disabled", "disabled");
                }); // Click
            });
        </script>
    </HEAD>

    <BODY>
        <input type='text' id="ayyappa"  name='ayyappa' value="ayyappa" disabled>
    </BODY>
</HTML>
like image 24
ayu for u Avatar answered Oct 13 '22 21:10

ayu for u