php - Send checked table row value in array

510

I have a table like this:

CheckBox    Course    Semester   Paper       Level
  []         ABC         1         1     •Expert •normal
  []         ABC         1         2     •Expert •normal
  []         ABC         1         3     •Expert •normal

 [Save]

In the above table checkbox value is paper value. The trouble i am having is that , when i click on save, it runs a parameterised javascript function addpapers($course,$semester) as you can see they are same, but i am not able to send the checkbox value and level value together. a person can choose either expert or normal level for a given paper. I am not able to send them in javascript.

function flagaddpapers(course, semester) {
    displayBox = document.getElementById("studentBox");
    elements = document.getElementsByName('pids[]');
    levelid = document.getElementsByName('radios[]');

    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) {
            document.body.removeChild(document.getElementById('loadMsg'));
            result = xmlhttp.responseText;
            alert(result);
            displayBox.className = "dimBox";
            setTimeout(function () {
                ajaxstp();
                document.body.removeChild(document.getElementById('adBackground'));
                document.body.removeChild(document.getElementById('adBox'));
            }, 200);
        }
    }
    xmlhttp.open("POST", "tpaper.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(params);
}

This is my html code:

echo "<tr>
<th style='font-size:12px;'><input type='checkbox' id='master_check'
onclick='masterSelect();' style='display:inline-block;cursor:pointer;'></th>
<th style='font-size:12px;'>S.No</th>
<th style='font-size:12px;'>Course</th>
<th style='font-size:12px;'>Semester</th>
<th style='font-size:12px;'>Level</th>
<th style='font-size:12px;'>Paper</th></tr>";
$counter17 = 1;
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' name='radio[]' id='radio1'
value='1'>Expert</input><input id='radio2' type='radio' name='radio[]' 
value='2'>Normal</input></td>";
echo "<td style='text-align:center;font-size:12px;'>".$row4['pname']."</td></tr>";
$counter17 = $counter17 + 1;
}
echo "</table>";
echo "<div style='text-align:left;'><input type='button' class='t_s' value='Add Paper'
onclick='flagaddpapers(".$course.",".$semester."); return false'
id='addpaperBtn'><input type='button' class='t_s' value='Cancel' onclick='closedialog();
return false'></div>";
341

Answer

Solution:

Try this.. herepval is thepaperid andlval is thelevel value

<script type="text/javascript">
function merge_value(lval,pval)
{
document.getElementById(pval).value=pval + '-' + lval;
}
</script>

<?php

echo "<tr>
<th style='font-size:12px;'><input type='checkbox' id='master_check'
onclick='masterSelect();' style='display:inline-block;cursor:pointer;'></th>
<th style='font-size:12px;'>S.No</th>
<th style='font-size:12px;'>Course</th>
<th style='font-size:12px;'>Semester</th>
<th style='font-size:12px;'>Level</th>
<th style='font-size:12px;'>Paper</th></tr>";
$counter17 = 1;
while($row4 = mysql_fetch_array($q4))
{
$pids = $row4['pid'];
echo "<tr>";
echo "<td><input type='checkbox' name='pids[]' id='".$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' name='radio[]' id='radio1'
value='1' onClick='javascript:merge_value(this.value,".$pids.")'>Expert</input><input id='radio2' type='radio' name='radio[]' 
value='2' onClick='javascript:merge_value(this.value,".$pids.")'>Normal</input></td>";
echo "<td style='text-align:center;font-size:12px;'>".$row4['pname']."</td></tr>";
$counter17 = $counter17 + 1;
}
echo "</table>";
echo "<div style='text-align:left;'><input type='button' class='t_s' value='Add Paper'
onclick='flagaddpapers(".$course.",".$semester."); return false'
id='addpaperBtn'><input type='button' class='t_s' value='Cancel' onclick='closedialog();
return false'></div>";

?>

People are also looking for solutions to the problem: favicon - Save png file locally from a URL in PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.