Skip to content Skip to sidebar Skip to footer

How To Get Value Of Dynamically Generated Textbox With Same Id Using AJAX/PHP?

In this webpage I am generating multiple textbox dynamically and each textbox is meant to hold unique value and I want to get that value dynamically.But I'm not being able to catch

Solution 1:

First you should use classes not id, because an element with id must be unique for the entire document. And since you use onChange you can pass the element using this like that onChange="checkusername(this)" . I guess you should also change the code of the restrict function onkeyup="restrict('serialArray')" also but i do not see that code so I cannot help you more if you do not provide this code too...

    <tr>
    <td  align="center"><input type="text" name="serialNoArray[]" class="serialArray" onkeyup="restrict('serialArray')" onChange="checkusername(this)" ><span class="std_id_status"></span></td>
    </tr>

Then you can get only the value of the element being changed and change the html of the matching span only.(I use jQuery in the example so you should include it in your document.)

<script>
    function checkusername(s) {

        if (s.value != "") {
            $(s).nextAll('.std_id_status').first().html('checking ...');
            var ajax = ajaxObj("POST", "sellingDetails.php");
            ajax.onreadystatechange = function() {
                if (ajaxReturn(ajax) == true) {
                  $(s).nextAll('.std_id_status').first().html(ajax.responseText);
                }
            }
            ajax.send("std_id_check=" + s.value);
        }
    }
</script>

Since i do not have all your javascript code I could not test it but something like this should work.


Solution 2:

I have not tested but this should do it

All the dynamically generated textboxes, give them a class

<input type="text" class="tagMe" placeholder="Enter Serial No."  onkeypress="return isNumberKey2(event)" onkeyup="restrict('serialArray')" onChange="checkusername()" required autofocus >

Collecting the data

var info= "";
$('.tagMe').each( obj, function( key, value ) {
 if(info != "")
      info += "^"; // ^ is a delimiter
 info += value; 
});

Send info to your server, split on ^ and parse data (careful of empty elements)


Post a Comment for "How To Get Value Of Dynamically Generated Textbox With Same Id Using AJAX/PHP?"