访问多个复选框的值

时间:2013-07-13 00:48:31

标签: php javascript html ajax forms

我想访问动态创建的复选框的值。

@ $db = mysql_connect("abc", "abc", "");
                            mysql_select_db("abc");

                            $strSQL = "SELECT * FROM student";
                            $rs = mysql_query($strSQL);
                            $num_rows = mysql_num_rows($rs);
                            echo "<i style='color:#fff'> Number of Students = ".$num_rows."</i>";
                            $i=1;
                            while($r = mysql_fetch_array($rs)){

                            echo "<tr>";
                            echo "<td class='promotetabledata'>".$r[7]."</td>";
                            echo "<td class='promotetabledata'>".$r[6]."</td>";
                            echo "<td class='promotetabledata'><input type='checkbox' class='pr'  value='".$r[7]."'/></td>"; /*dynamically created check boxes*/
                            echo "</tr>";
                            $i++;
                }   

结果显示在使用AJAX的promoteresults div

<form id="promotionform" action="promotestudents.php" method="POST">
            <div id="promoteresults">The results will show up here..!!
            </div>


            <div style=" position:relative; margin-top:10px; padding-left:44%;">  
                <button type="submit" class="button black">Promoted</a>
            </div>
    </form>

单击提升按钮时,我想获取所选记录并更新其值。要更新记录,我需要PHP。我可以使用

在Javascript中访问所选记录

var selected = document.getElementsByClassName('pr').checked;

但是如何在HTML表单中获取已检查的记录及其在PHP中的值

使用Javascript进行AJAX调用

function getpromotestudents()
{
//alert("hi");

var xmlhttp;
var select1 = document.getElementById('promotefacultyselect1'); 
var facutlyselect = select1.options[select1.selectedIndex].value;


var select2 = document.getElementById('promotedepartmentselect1'); 
var deptselect = select2.options[select2.selectedIndex].value;


var select3 = document.getElementById('promotecourseselect1'); 
var courseselect = select3.options[select3.selectedIndex].value;


var select4 = document.getElementById('promoteyearselect1'); 
var yearselect = select4.options[select4.selectedIndex].value;


var select5 = document.getElementById('promotesemselect1'); 
var semselect = select5.options[select5.selectedIndex].value;


var  the_data = 'faculty='+facutlyselect+' &dept='+deptselect+' &course='+courseselect+' &year='+yearselect+' &sem='+semselect;


if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  /*
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("searchresults").innerHTML=xmlhttp.responseText;
    }
  }*/



  xmlhttp.open("POST", "getpromotestudents.php", true);         // set the request
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");            // adds  a header to tell the PHP script to recognize the data as is sent via POST
  xmlhttp.send(the_data);       // calls the send() method with datas as parameter

  // Check request status
 // If the response is received completely, will be transferred to the HTML tag with tagID
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
      document.getElementById("promoteresults").innerHTML = xmlhttp.responseText;
    }
  }
}

3 个答案:

答案 0 :(得分:1)

 <td class='promotetabledata'><input type='checkbox' name='pr[]'  value='".$r[7]."'/></td>   

这是我最终用来访问所有复选框的PHP代码,并对选定的复选框执行所需的操作。

<?php
    $checkbox = $_POST['pr'];

    foreach($checkbox as $value){

            if(isset($checkbox)){

            echo '<script>alert("'.$value.'")</script>';
            }
    }
    ?>

答案 1 :(得分:0)

将每个复选框的name属性设置为pr[],如下所示:

echo "<input type='checkbox' class='pr' name='pr[]' value='".$r[7]."'/>";

然后,第一个复选框位于$_POST['pr'][0],第二个位于$_POST['pr'][1],依此类推。

答案 2 :(得分:0)

像这样checkbox name属性

<input type='checkbox' class='pr'  value='8' name='chk[]'/>

比在php中你可以得到它们如下

<?php
 $chkbox = $_POST['chk'];

 foreach($chkbox as $a => $b){
    echo $chkbox[$a];
 }
?>