dongxing5525 2016-04-21 20:26
浏览 19

只有选中的复选框才能执行进一步的ajax查询

I have 7 checkbox, of which i want values of only selected checkbox to make further ajax query to insert data in database.

enter image description here

Javascript must initialize the value of selected checkbox and pass them forward to the php file were insertion is going to be made. As I am new to ajax, I am unable to understand were am I making mistake. Here is the code that I currently have.

`<script type="text/javascript">
$(document).ready(function(e) {
$("#askquestion").click(function(){

var alll = $('#alll').val();
var bff = $('#bff').val();
var relative = $('#relative').val();
var coligues = $('#coligues').val();
var acquintance = $('#acquintance').val();
var friends = $('#friends').val();
var follower = $('#follower').val();
var qid = $('#qid').val();


 $.ajax({
          type: "POST",
          url: "ajaxaskquestion.php",
          data : {
              alll:alll,
              bff:bff,
              relative:relative,
              coligues:coligues,
              acquintance:acquintance,
              friends:friends,
              follower:follower,
              qid:qid
              },success: function(data){
                    alert("done");
                },
      error: function (data){
          ("not done");
      }
 });
 });
 });

session_start();
require_once("connect.php");

 $alll = $_POST["alll"];
 $bff = $_POST["bff"];
 $friends = $_POST["friends"];
 $coligues = $_POST["coligues"];
 $relative = $_POST["relative"];
 $acquintance = $_POST["acquintance"];
 $follower = $_POST["follower"];
 $qid=$_POST["qid"];

if(isset($_POST)){

   if(isset($alll)){
// perform some function
}

else{
      if(isset($bff)){
        // perform some function

}
      if(isset($friends)){
        // perform some function

}
//and so on..
}
}
 else{
 echo "Question not sent";
 }

I know the method which I used is very long and far away from being subtle. Any and every suggestion is welcomed. Thank you

  • 写回答

1条回答 默认 最新

  • doujingtang6580 2016-04-21 20:55
    关注

    what about using jquery's own :checked selector?

    $.ajax({
              type: "POST",
              url: "ajaxaskquestion.php",
              data : {
                  alll: $("#alll").is(":checked"),
                  bff:$("#bff").is(":checked"),
    
                  /* ... and so on */
                  },
              success: function(data){
                  alert("done");
              },
              error: function (data){
                  ("not done");
              }  
     });
    


    then in your PHP file:

    if($_POST["all"])
    {
        // do stuff
    }
    /* and so on */
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?