【问题标题】:PHP Batch Processing: send checkbox and radio values of table rows togetherPHP批处理:一起发送表格行的复选框和单选值
【发布时间】:2013-05-26 06:11:02
【问题描述】:

我正在使用 mysql 查询的结果生成一个表,每行中都有复选框和单选按钮,例如:

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 将选中的行的值与选中的行的单选按钮值一起发送到 PHP 页面以插入 MYSQL。

我知道如何使用 JS 的 pushjoin 函数仅发送选中的复选框值,但在发送选中行的相关单选按钮值的过程中遇到了困难。

我使用的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。

我应该如何发送选中复选框的单选按钮值?我是否需要在 JS 中运行嵌套循环,然后在 PHP foreach 中运行?

请帮忙...

【问题讨论】:

  • 你只能在 php 中完成..

标签: php ajax batch-processing


【解决方案1】:

在收集复选框的同时收集另一个数组中的单选值。

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;
            }
        }
    }
}

并将它们添加到查询字符串中:

params= "teacher="+encodeURIComponent(teacher)+"&course="+encodeURIComponent(course)+"&semester="+encodeURIComponent(semester)+"&flag="+encodeURIComponent(1)+"&"+data.join('&')+&+radio_data.join('&');

【讨论】:

  • 这只会收集那些复选框已被选中的单选值?正确的?然后在 PHP 中,我应该为单选按钮运行单个 foreach 循环还是嵌套循环
  • 另外,在 while 循环中,如果我将单选按钮命名为 radios[],它将在所有生成的行之间共享,这意味着我一次只能单击一个。
  • 是的,因为它只在if(checked) 正文中添加单选值,它只会发送与复选框相关联的值。 PHP中的单个循环应该可以同时获取它们。
  • 当您使用以[] 结尾的名称时,PHP 会创建一个包含所有值的数组。所以$_GET['pids']$_GET['radio'] 将是数组,你可以检查任意多个。
  • 不不,我是说如果我将所有行的单选按钮命名为radio[],它只允许检查所有行的一个单选。我该如何解决?
猜你喜欢
  • 1970-01-01
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 2015-11-03
  • 2015-09-13
相关资源
最近更新 更多