问题描述
我正在使用mysql查询的结果生成一个表,其中每行都带有复选框和单选按钮,如:
I am generating a table using the result of mysql query with checkboxes and radio buttons in each row like:
while($row4 = mysql_fetch_array($q4))
{
$pids = $row4['pid'];
echo "<tr>";
echo "<td><input type='checkbox' name='pids[]' class='persistentChecks' style='display:inline-block;cursor:pointer;' value=".$pids."></td>";
echo "<td style='text-align:center;font-size:12px;'>".$counter17."</td>";
echo "<td style='text-align:center;font-size:12px;'>".$row4['cname']."</td>";
echo "<td style='text-align:center;font-size:12px;'>".$semester."</td>";
echo "<td style='font-size:12px;'><input type='radio' value='1'>Expert<input type='radio' value='2'>Normal";
echo "<td style='text-align:center;font-size:12px;'>".$row4['pname']."</td>";
echo "</tr>";
$counter17 = $counter17 + 1;
}
这是一个批处理问题.我想通过ajax将选中的行的值与选中的行的单选按钮值一起通过ajax发送到PHP页,以便在MYSQL中插入.
Its a batch processing problem. I want to send the values of the rows checked along with the radio button value of that checked row through ajax to a PHP page for insertion in MYSQL.
我知道如何仅使用JS的push
和join
函数发送已选中的复选框值,但是在发送已选中行的相关单选按钮值的过程中遇到了麻烦.
I know how to send only checked checkbox values with push
and join
functions of JS but am stuck on the way to send the associated radio button values of the checked rows along.
我正在使用的JS是:
data = [];
for (i = 0; i < elements.length; i++){
if (elements[i].checked){
data.push('pids[]='+encodeURIComponent(elements[i].value));
}
}
params= "teacher="+encodeURIComponent(teacher)+"&course="+encodeURIComponent(course)+"&semester="+encodeURIComponent(semester)+"&flag="+encodeURIComponent(1)+"&"+data.join('&');
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
result = xmlhttp.responseText;
alert(result);
}
}
xmlhttp.open("POST","tpaper.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(params);
现在在PHP页面上,我正在使用foreach循环遍历上述JS发送的pid.
Now on the PHP page, i am looping through the pids sent through JS above using a foreach loop.
我还应该如何发送选中的复选框的单选按钮值?我是否需要在JS中然后在PHP foreach
中也运行嵌套循环?
How am i supposed to send the radio button values of the checked checkboxes as well? Do i need to run a nested loop in JS and then in PHP foreach
as well?
请帮助...
推荐答案
在收集复选框的同时,在另一个数组中收集单选值.
Collect the radio values in another array at the same time as you collect the checkboxes.
data = [];
radio_data = [];
for (i = 0; i < elements.length; i++){
if (elements[i].checked){
var pid = elements[i].value;
data.push('pids[]='+encodeURIComponent(pid));
var radios_elements = document.getElementsByName('radio'+pid);
for (var j = 0; j < radio_elements.length; j++) {
if (radio_elements[j].checked) {
radio_data.push('radios[]='+encodeURIComponent(radio_elements[j].value));
break;
}
}
}
}
并将它们添加到查询字符串中:
And add them to the query string:
params= "teacher="+encodeURIComponent(teacher)+"&course="+encodeURIComponent(course)+"&semester="+encodeURIComponent(semester)+"&flag="+encodeURIComponent(1)+"&"+data.join('&')+&+radio_data.join('&');
这篇关于PHP批处理:一起发送复选框和表行的单选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!