Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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...

share|improve this question
You can do it in php only.. – sAnS May 26 at 6:28

1 Answer

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('&');
share|improve this answer
this will only collect those radio values for whom the checkboxes have been checked? right? and then in PHP, am i supposed to run a single foreach loop or a nested one for radio buttons as well – coder101 May 26 at 6:35
Also, in the while loop, if i am giving the radio buttons the name as radios[], it is being shared among all generated rows meaning i can click only one at a time. – coder101 May 26 at 6:37
Yes, since it only adds the radio values inside the if(checked) body, it will only send the ones associated with the checked boxes. A single loop in PHP should be able to get both of them. – Barmar May 26 at 6:37
When you use a name that ends in [], PHP creates an array of all the values. So $_GET['pids'] and $_GET['radio'] will be arrays, and you can check as many as you want. – Barmar May 26 at 6:38
no no, i am saying that if i name the radio buttons as radio[] for all rows, it allows only one radio to be checked for all rows. how do i solve that? – coder101 May 26 at 6:42
show 23 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.