当表单的每一行具有不同的name属性时,PHP循环数据 [英] PHP Looping data when a form have a different name attribute every each row
问题描述
当我创建的显示数据表单需要不同的name
属性时,如何进行数据循环?
How do I make a loop of data when the form I created the data displayed requires a different name
attribute ?
我制作了一个jQuery代码来创建像这样的元素:
I make a jQuery code to create elements like:
$("#loadDataSiswa").append(""+
"<div class='grid_2xxx kekiri'>" + nm_depan + "</div>"+
"<div class='grid_5xxx kekanan'> \n"+
"<input type='radio' name='"+id+"' value='1'> Masuk"+
"<input type='radio' name='"+id+"' value='2'> Tidak Masuk"+
"<input type='radio' name='"+id+"' value='3'> Sakit"+
"<input type='radio' name='"+id+"' value='4'> Tidak Ada Kabar"+
"</div>"+
"");
id
和nm_depan
值已经通过ajax进程完成.
id
and nm_depan
values already done with ajax process.
如果您看上面的代码,我必须使用不同的属性名称来使它显示的表单应该像这样
If you look at the code above, I have to make it with a different attribute name for the form to be displayed should be like this
我创建的表格用于产生应在每一行中选择的单选元素
Form I created to produce radio element that should be selected each row
foreach($_POST as $val) {
// Insert to DB
}
我在这里遇到了问题.如果选择了特定的id
值,如何进行循环以获取id
的值?在PHP中如何使用不同的name属性循环?因为在这里,我使用从ajax获得的id
来更改每个属性的名称.
Here I get into trouble. How do I make a loop to get the value of id
will I do insert into the database if a particular id
selected value? How does in PHP to loop with a different name attribute? Because here, I'm changing the name of each attribute using the id
that I get from the ajax.
推荐答案
可以通过将隐藏的输入与某个广播组相关联来解决您的问题.
Your problem can be resolved associating a hidden input with some radio group.
添加具有id
值的隐藏输入:
Add a hidden input with id
value:
"<input type='hidden' name='radioList[]' value='" + id + "'" />"
此输入是一个数组,因此您可以迭代.
This input is an array, so you can iterate through.
下一步,您必须修改您的广播组以与您的id
关联:
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
这篇关于当表单的每一行具有不同的name属性时,PHP循环数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!