PHP Batch Processing: send checkbox and radio values of table rows together

狂风中的少年 提交于 2019-12-12 04:56:22

问题


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;

 }

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.

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.

the JS i'm using is:

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

Now on the PHP page, i am looping through the pids sent through JS above using a foreach loop.

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?

Please help...


回答1:


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('&');


来源:https://stackoverflow.com/questions/16756704/php-batch-processing-send-checkbox-and-radio-values-of-table-rows-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!