0

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
  • You can do it in php only.. Commented May 26, 2013 at 6:28

1 Answer 1

0

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('&');
Sign up to request clarification or add additional context in comments.

27 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.