Working on some AJAX code. Need to get the value of a form array to send over to the PHP handler. Using the usual document.getElementById does not work.

Here is a form field example.

<input type="text" name="quantity[]" class="input_01" style=" width:15px;" onkeypress="return isNumberKey(event)" id="quantity[]" />

These are repeated several times. I have it going to a PHP handler directly now and I pickup the arrays using $_REQUEST but need to do some AJAX on the values before submitting.

Thanks

    Are you using jQuery to do the AJAX call? In any case, how your PHP handles the form input is not affected by how it gets there. In other words whether you're submitting your form via AJAX or simply just reloading the page the PHP code will be the same.

    Getting values from your form via jQuery can be accomplished in a number of ways. The easiest being:

     var formData = $('yourformselector').serialize(); 
       <input type="text" name="quantity[]" class="input_01" style=" width:15px;" onkeypress="return isNumberKey(event)" id="quantity[]" />

      Why are you using [font=monospace][][/font] array brackets to name the quantity? Is it because there can be multiple copies of this "quantity" field?

      If the answer is "yes", then [font=monospace]id="quantity[]"[/font] won't work, because all id attributes within a document have to be unique. If you do need to refer to these fields individually (if Bonesnap's suggestion isn't enough for some reason) then you'll need to make sure their ids are unique (by using array indices, for example: [font=monospace]quantity[0][/font], [font=monospace]quantity[1][/font], [font=monospace]quantity[2][/font], ...).

        Yes there are multiple copies of the "quantity" field (along with description and price). It is JQuery that we are using for the AJAX. Using the empty [] assigns the next index to the id (at least that what this script is doing. There can be an unlimited number of items each with a quantity, description, and price.

        Bonesnap, can you elaborate a bit? We are not reloading the page. When complete the form submits to the handler. We are attempting to use AJAX on the data prior to submission of the form.

        Weedpacket;11053885 wrote:
         <input type="text" name="quantity[]" class="input_01" style=" width:15px;" onkeypress="return isNumberKey(event)" id="quantity[]" />

        Why are you using [font=monospace][][/font] array brackets to name the quantity? Is it because there can be multiple copies of this "quantity" field?

        If the answer is "yes", then [font=monospace]id="quantity[]"[/font] won't work, because all id attributes within a document have to be unique. If you do need to refer to these fields individually (if Bonesnap's suggestion isn't enough for some reason) then you'll need to make sure their ids are unique (by using array indices, for example: [font=monospace]quantity[0][/font], [font=monospace]quantity[1][/font], [font=monospace]quantity[2][/font], ...).

          fotofx wrote:

          Yes there are multiple copies of the "quantity" field (along with description and price).

          Hence

          Weedpacket wrote:

          ...then id="quantity[]" won't work, because all id attributes within a document have to be unique...you'll need to make sure their ids are unique (by using array indices, for example: quantity[0], quantity[1], quantity[2], ...).

            Write a Reply...