php - 使用 javascript Ajax 而不是 jQuery 将复选框值发送到 PHP 页面

标签 php mysql html ajax

我遇到一种情况,我需要对通过发送到 php 脚本的 ajax 请求选中的每个复选框进行插入查询,该脚本将在 mysql 中执行插入操作。

我知道如何在没有 ajax 调用的情况下通过简单的表单提交来完成此操作,使用像 groups[] 这样的变量作为数组,并在 php 中为数组中的每个值运行 foreach 循环。

如何通过 ajax 请求发送数组?

示例代码:

<input type='checkbox' name='groups[]' value='1'>Group A
<input type='checkbox' name='groups[]' value='2'>Group B
<input type='checkbox' name='groups[]' value='3'>Group C

请帮忙,我知道这可能很容易,但我只是不明白。伙计们,请不要给出任何 jquery 或类似的例子,因为我想要纯 html、javascript 和 php 解决方案。

感谢社区...

这是 Javascript 函数:

<script type='text/javascript'>
function addResp(tid){

a = encodeURIComponent(document.getElementById('course_add_resp').value);
b = encodeURIComponent(document.getElementById('term_add_resp').value);
c = encodeURIComponent(document.getElementById('paper_add_resp').value);

var elements = document.getElementsByName('groups[]');  
var data = [];
for (var i = 0; i < elements.length; i++){
if (elements[i].checked){
    data.push('groups[]='+elements[i].value);
   }
}
params = "tid="+tid+"&course="+a+"&sem="+b+"&paper="+c+"&grp="+data;
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('respBackground'));
document.body.removeChild(document.getElementById('respBox'));      
contents = xmlhttp.responseText;    
if(contents == "done"){
window.location = "teachers.php";
} else{
document.getElementById("studentBox").innerHTML = "There was a problem serving the     request.";
 }

    }
  }
 xmlhttp.open("POST","assignresp.php",true);
 xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 xmlhttp.send(params);

 }

</script>

和 php 脚本:

  <?php

  $tid = mysql_real_escape_string($_POST['tid']);
  $cid = mysql_real_escape_string($_POST['course']);
  $sem = mysql_real_escape_string($_POST['sem']);
  $paper = mysql_real_escape_string($_POST['paper']);
  $session = 12;
  $type = 1;

  $groups = $_POST['grp'];

  foreach ($groups as $value ) {
      $q1 = "insert into iars(sessionid,teacherid,courseid,semester,paperid,groupid,type)    values('$session','$tid','$cid','$sem','$paper','$value','$type')";
      $r1 = mysql_query($q1) or die(mysql_error());
      if(mysql_affected_rows() > 0){
          echo "done";
      }
      else{
          echo "fail"; 
      } 
  }


 ?>

最佳答案

var elements = document.getElementsByName('groups[]');  
var data = [];
for (var i = 0; i < elements.length; i++){
    if (elements[i].checked){
        data.push('groups[]='+elements[i].value);
    }
}
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(data.join('&'));
//for get
//xmlhttp.open("GET",url+"?"+data.join('&'),true);
//xmlhttp.send();

编辑

更改这两行。

params = "tid="+tid+"&course="+a+"&sem="+b+"&paper="+c+"&"+data.join('&');

$groups = $_POST['groups'];

关于php - 使用 javascript Ajax 而不是 jQuery 将复选框值发送到 PHP 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11681570/

相关文章:

mysql - 统计关键字(在一个表中)在另一个表的字段中出现的次数

php - 如何从 http 更改为 https?

php - Symfony Controller 功能测试中的 $this->getUser() 返回 null

java - 相同类型的代码,但有一个代码显示异常

mysql - 如何在Sql查询中使用GROUP BY子句来显示具有多个值的唯一数据?

HTML 元素没有正确地垂直对齐到中间

android - PhoneGap Android 应用程序中的签名捕获

html - 向文档添加 &lt;script&gt; 会导致它无法加载文档的其余部分

PHP MYSQL 连接错误

php - 应采用什么设计来保存文件信息