PHP Batch Processing: send checkbox and radio valu

2019-08-20 23:43发布

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条回答
男人必须洒脱
2楼-- · 2019-08-21 00:00

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('&');
查看更多
登录 后发表回答