Skip to content Skip to sidebar Skip to footer

PHP Looping Data When A Form Have A Different Name Attribute Every Each Row

How do I make a loop of data when the form I created the data displayed requires a different name attribute ? I make a jQuery code to create elements like: $('#loadDataSiswa').appe

Solution 1:

Your problem can be resolved associating a hidden input with some radio group.

Add a hidden input with id value:

"<input type='hidden' name='radioList[]' value='" + id + "'" />"

This input is an array, so you can iterate through.

On next step you have to modify your radio group to associate with your id:

"<input type='radio' name='myradio["+id+"]' value='1'> Masuk"+
"<input type='radio' name='myradio["+id+"]' value='2'> Tidak Masuk"+
"<input type='radio' name='myradio["+id+"]' value='3'> Sakit"+
"<input type='radio' name='myradio["+id+"]' value='4'> Tidak Ada Kabar"+

So in your code you should be able to do:

//Iterate through any inserted id from a group
foreach($_POST['radioList'] as $id)
{
    //if you didn't let any pre checked value, you need use this if
    if(isset($_POST['myradio'][$id])
    {
        echo $_POST['myradio'][$id];//checked value this group
    }
}

After all you should be able to have any quantity of radio buttons associated to an infinity list


Solution 2:

To get the keys in the post var try this. $key holds the name attribute (javascript id var) from the Form.

foreach($_POST as $key => $value){

    if($key == "whatever") ....

}

Solution 3:

You can use partial name selectors. Please refer to this https://api.jquery.com/attribute-contains-selector/ Or you can give a class to the inputs just to loop them.


Post a Comment for "PHP Looping Data When A Form Have A Different Name Attribute Every Each Row"