1

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

@ $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++;
                }   

结果以promoteresults div使用 AJAX的方式显示

<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;
    }
  }
}
4

3 回答 3

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>';
            }
    }
    ?>
于 2013-07-13T02:08:24.003 回答
0

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

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

然后,第一个复选框将在$_POST['pr'][0],第二个在$_POST['pr'][1]等等。

于 2013-07-13T00:58:13.393 回答
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];
 }
?>
于 2013-07-13T01:14:44.860 回答