1

I have one html form

<form>
<input type="hidden" name="id" value="1" /><input type="text" name="qty" />
<input type="hidden" name="id" value="2" /><input type="text" name="qty" />
<input type="hidden" name="id" value="3" /><input type="text" name="qty" />
<input type="hidden" name="id" value="4" /><input type="text" name="qty" />
<input type="submit"/>
</form>

When I submit form,Say for id = 1 , qty is 5 or for id = 3, qty is 8. How to get this values in java script or jquery and submit this information to server? I get all the input text values. But I cannot differentiate which qty is for which value?

6
  • Why do you have to give the same id and and name to all the inputs? What Is your server platform? Commented Jul 3, 2015 at 7:07
  • Server platform is JAVA Commented Jul 3, 2015 at 7:08
  • 1
    not really clear, can you explain with more details what you want? User will type something in the text field, and then? why are your other inputs hidden? what are they supposed to do? When user types 8 then id should be 1? I really do not understand what you mean... And, about submitting the form, you should simply do it with php... If not, then do it through an ajax call... Commented Jul 3, 2015 at 7:08
  • Actually I want this, user can type any quantity. By using java script I can get all the quantity. But I want, for Id = 1, what is the quantity or for Id = 2 what is the quantity Commented Jul 3, 2015 at 7:11
  • What is it supposed to do? You try to get the quantity that user types for a specific product or something? Then, why don't you just name your input text like "qtyOne","qtyTwo", etc... ?^^ Commented Jul 3, 2015 at 7:13

4 Answers 4

4

You should not have two or more elements with the same name or id! names & ids are meant to be unique.

try this:

<form>
<input type="hidden" name="id[1]" value="1" /><input type="text" name="qty[1]" id="qty1" value="" />
<input type="hidden" name="id[2]" value="2" /><input type="text" name="qty[2]" id="qty2" value="" />
<input type="hidden" name="id[3]" value="3" /><input type="text" name="qty[3]" id="qty3" value="" />
<input type="hidden" name="id[4]" value="4" /><input type="text" name="qty[4]" id="qty4" value="" />
<input type="submit"/>
</form>

You can acces the data in JS like this:

using the element name:

var qty1 = document.forms[0].elements["qty[1]"].value;

Using the element id:

var qty1 = document.getElementById("qt1").value;
Sign up to request clarification or add additional context in comments.

5 Comments

Not quite true - id must be unique, but names don't have to be (there are actually scenarios where the same name is used, like radio groups)
Sidenote: As he asked on how to submit to the server, there is the action and method attribute in the form missing
When using multiple checkboxes for instance, you might want to group them by the name, so that when one is checked, the other toggle. But I agree for the ID part.
ok names can be multiple but not for text and hidden types..... except in some frameworks...
The square bracket notation for form fields is used by PHP. OP did not mention that he is using PHP.
2

Well, to me, your hidden inputs are useless : You say that for input text one is a corresponding hidden field id with a defined value. Then you just have to name your input correctly and you know which id or something is correspondant.

If you want to use jQuery (javascript) as you said, then just do something like this :

<form>
    <input type="text" name="qty1" id="qty1" />
    <input type="text" name="qty2" id="qty2" />
    <input type="text" name="qty3" id="qty3" />
    <input type="text" name="qty4" id="qty4" />
    <input type="submit" name='submit' id="submitButton" value="submit"/>
</form>

<script>
    $('document').on('click','#submitButton',function(event){
        event.preventDefault();
        //this prevents the click to submit, as you want to get values by javascript... remove this if you post it with php
        console.log('QTY1 : '+$('#qty1').val()+' - QTY2 : '+$('#qty2').val()+' - QTY3 : '+$('#qty3').val()+' - QTY 4 : '+$('#qty4').val());
        //This is with jQuery, javascript would look like addEventListeneer, and get values like document.getElementById('id').value;
        //then, ajax something if you really want to post your form through javascript, but this is not a good solution...
    });
</script>

Other solution, better to me, would be just to use php and post directly your form...

<?php
    if(isset($_POST['submit'])){
        //then, user has clicked on submit button
        //CONSIDERED THAT YOU HAVE CHECKED IF THE FIELDS ARE NOT EMPTY
        $qty1 = $_POST['qty1'];
        $qty2 = $_POST['qty2'];
        $qty3 = $_POST['qty3'];
        $qty4 = $_POST['qty4'];
        //do your stuff with the posted values...
    }else{
        echo '
            <form method="POST" action="yourPage.php">
                <input type="text" name="qty1" id="qty1" />
                <input type="text" name="qty2" id="qty2" />
                <input type="text" name="qty3" id="qty3" />
                <input type="text" name="qty4" id="qty4" />
                <input type="submit" name="submit" id="submitButton" value="submit"/>
            </form>
        ';
    }
?>

Hope it helps

Comments

1

Your html is perfectly valid, but you're not using the name of the elements to your advantage.

<input type="hidden" name="id1" value="1" /><input type="text" name="qty1" />

This will return:

id1=1&qty1=X

But in the end I think your hidden field is just wasting space. All you seem to need is the quantity field with a proper name.

<input type="text" name="qty1" />
<input type="text" name="qty2" />
...

If you use the same name for the elements it will still give you the desired data, but it might need some parsing to figure out "what is what".

<input type="text" name="qty" />
<input type="text" name="qty" />
... 

This will basically return:

     qty=X,Y   (or qty=X&qty=Y)
item ----0-1

Comments

1

You could wrap each pair of hidden-text inputs in div element, then iterate through these divs and find these two inputs inside each of the div elements. It might look like this:

<form>
  <div id="keyValuePairs">
    <div class="pair">
      <input type="hidden" name="id" value="1" /><input type="text" name="qty" />
    </div>
    <div class="pair">
      <input type="hidden" name="id" value="2" /><input type="text" name="qty" />
    </div>
    <div class="pair">
      <input type="hidden" name="id" value="3" /><input type="text" name="qty" />
    </div>
    <div class="pair">
      <input type="hidden" name="id" value="4" /><input type="text" name="qty" />
    </div>
  </div>
  <input type="submit" id="sendBtn"/>
</form>

Then iterate with Jquery

$("#sendBtn").click(function(){
  var arr = [];
  $("#keyValuePairs .pair").each(function(){
    var val = $(this).find("input[name='id']").val();
    var key = $(this).find("input[name='qty']").text();
    var obj = { val:val, key:key };
    arr.push(obj);
  });
  // some ajax call or any way you want to post data server
});

at the end of iteration, arr should contain objects with val property containing what is in the value property of hidden input, and key property containing what the text present in text inputs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.