使用ajax php mysqli更新复选框

时间:2016-01-26 15:13:16

标签: php jquery mysql ajax

这是我的模态

 <div class="modal fade bs-example-modal-lg" id="exampleModal<?php echo $row['t_id'] ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel<?php echo $row['t_id'] ?>">
   <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title" id="exampleModalLabel">Termin Ansicht <font color="red">ID</font>: <b><?php echo $row['t_id'] ?></b> <font color="red">Call Agent</font>: <b><?php echo $row['username'] ?></b>  <font color="red">Call Datum</font>: <b><?php echo date("d.m.y" , strtotime($row['datum'])) ?></b></h4>
  </div>
  <div class="modal-body">
          <form>
                              <div class="row">

                                  <div class="col-sm-3">
                                      <input type="text" id="username" value="<?php echo $row['username']; ?>"  class="form-control">
                                  </div>
                                <div class="col-sm-3">
                                      <input type="text" id="t_id" value="<?php echo $row['t_id']; ?>"  class="form-control">
                                </div>


                              </div>
                              <br>

                            <div class="row">
                              <label class="col-sm-1 control-label">Anrede</label>
                            <div class="col-sm-2">
                                    <input type="text" id="anrede<?php echo $row['t_id']; ?>" value="<?php echo $row['anrede']; ?>" class="form-control">
                            </div>
                                  <label class="col-sm-1 control-label">Vorname</label>
                                  <div class="col-sm-3">
                                      <input type="text"  id="vorname<?php echo $row['t_id']; ?>" value="<?php echo $row['vorname']; ?>"  class="form-control" onselectstart="return false" onpaste="return false;" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false">
                                  </div>
                                <label class="col-sm-1 control-label">Nachname</label>
                                <div class="col-sm-3">
                                      <input type="text" id="nachname<?php echo $row['t_id']; ?>" value="<?php echo $row['nachname']; ?>"  class="form-control" onselectstart="return false" onpaste="return false;" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false">
                                </div>
                              </div>
                              <br>
                            <div class="row">
                                  <label class="col-sm-1 control-label">Strasse</label>
                                  <div class="col-sm-3">
                                      <input type="text" id="strasse<?php echo $row['t_id']; ?>" value="<?php echo $row['strasse']; ?>"  class="form-control">

                                  </div>
                                <label class="col-sm-1 control-label">PLZ</label>
                                <div class="col-sm-3">
                                      <input type="text" id="plz<?php echo $row['t_id']; ?>" value="<?php echo $row['plz']; ?>"  class="form-control">
                                </div>

                                <label class="col-sm-1 control-label">Ort</label>
                                <div class="col-sm-2">
                                      <input type="text" id="ort<?php echo $row['t_id']; ?>" value="<?php echo $row['ort']; ?>"  class="form-control">
                                </div>
                              </div>
                              <br>
                             <div class="row">
                                  <label class="col-sm-1 control-label">Krankenkasse</label>
                                  <div class="col-sm-3">
                                      <input type="text" id="krankenkasse<?php echo $row['t_id']; ?>" value="<?php echo $row['krankenkasse']; ?>"  class="form-control">

                                  </div>
                                <label class="col-sm-1 control-label">Seit</label>
                                <div class="col-sm-3">
                                      <input type="text" id="seit<?php echo $row['t_id']; ?>" value="<?php echo $row['seit']; ?>"  class="form-control">
                                </div>

                                <label class="col-sm-1 control-label">Personen</label>
                                <div class="col-sm-1">
                                      <input type="text" id="personen<?php echo $row['t_id']; ?>" value="<?php echo $row['personen']; ?>"  class="form-control">
                                </div>
                              </div>
                              <br>


                              <div class="row">
                                  <label class="col-sm-1 control-label">Telefon</label>
                                  <div class="col-sm-2">
                                      <input type="text" id="telefon<?php echo $row['t_id']; ?>" value="<?php echo $row['telefon']; ?>"   class="form-control">

                                  </div>
                                <label class="col-sm-1 control-label">Termin</label>
                                <div class="col-sm-2">
                                      <input type="date" id="termin<?php echo $row['t_id']; ?>" value="<?php echo $row['termin']; ?>"  class="form-control">
                                </div>

                                <label class="col-sm-1 control-label">Uhrzeit</label>
                                <div class="col-sm-2">
                                      <input type="text" id="time<?php echo $row['t_id']; ?>" value="<?php echo  date("H:i", strtotime($row['time'])) ?>" class="form-control">
                                </div>

                                <div class="col-sm-2">
                                    <select class="form-control" id="vermittler<?php echo $row['t_id']; ?>" value='<?php echo $row['vermittler'];?>'>
                                        <option ><?php echo $row['vermittler'];?></option>
                                    <?php
                                    $result1 = ("SELECT * FROM users where type='berater'") or mysql_error(); 
                                $ilaz=mysql_query($result1);
                                while($rowa = mysql_fetch_array($ilaz)){

                                ?>
                                <option id="berater" value = "<?php echo $rowa['username']; ?>" >
                                    <?php echo $rowa['username']; ?>
                                </option>
                                <?php
                                }
                                ?>


                                        </select>  
                                        </div>
                              </div>

                            <br>


                               <div class="row">
                                      <label for="ccomment" class="control-label col-lg-2">Bemerkung</label>
                                      <div class="col-lg-4">
                                          <textarea class="form-control "  id="coment<?php echo $row['t_id']; ?>" required><?php echo $row['coment']; ?></textarea>
                                      </div>

                                      <label for="ccomment" class="control-label col-lg-1">Feedback</label>
                                      <div class="col-lg-4">
                                          <textarea class="form-control "  id="feedback<?php echo $row['t_id']; ?>" ></textarea>
                                      </div>
                                  </div>
                                    <br>

                                    <div class="row">
                                   <?php

                                   $bfeedback = ("select id, user, feedback_b, datum from b_feedback where id='". (int)$_GET['t_id']."' order by datum desc" ) or mysql_error();

                                   $bresult = mysql_query($bfeedback);
                                   if($bfeedback === FALSE) { 
                                            die(mysql_error()); // TODO: better error handling
                                        }

                                   while($beraterfeedback = mysql_fetch_array($bresult)){
                                       ?>
                                       <label for="b_feedback" class="control-label col-lg-2"><?php #echo $beraterfeedback['user'] ?><?php #echo $beraterfeedback['datum'] ?></label>

                                        <td><font color="red"><?php echo $beraterfeedback['user'] ?></font></td>&nbsp;&nbsp;&nbsp;&nbsp;
                                        <td><?php echo $beraterfeedback['datum'] ?></td>&nbsp;&nbsp;&nbsp;&nbsp;
                                        <td><?php echo $beraterfeedback['feedback_b'] ?></td><br>&nbsp;&nbsp;&nbsp;&nbsp;<hr>

                                       <?php

                                   }

                                   ?>


                                  </div>

                                <br>    
                        <div class="row">

                                <div class="col-sm-20">
                                  <?php
                                        $iAstatus = explode(",",$row['Astatus']);
                                        foreach($arrAstatus as $aAstatus)
                                        {
                                            if(in_array($aAstatus,$iAstatus))
                                                echo "<label class='col-sm-1 control-label'><input type='checkbox' class='s'  value='".$aAstatus."'  checked> .$aAstatus. </label>";                    
                                            else
                                                echo "<label class='col-sm-1 control-label'><input type='checkbox' class='s'  value='".$aAstatus."'> .$aAstatus.</label>" ;                 

                                        }
                                  ?>
                                </div>

                                    <div class="col-sm-1">
                                          <input type="text" id="positiv<?php echo $row['t_id']; ?>" value="<?php echo $row['abschluss']; ?>"  class="form-control">
                                    </div>

                            </div>
                            <br>
                                    <div class="row">
                                  <label class="col-sm-5 control-label"><p style="color:red"><font size="2">Personen Angaben (Namen, Geburtsdatum)?</font></p></label>
                                  <div class="col-sm-5">
                                          <textarea class="form-control " id="personen_angaben<?php echo $row['t_id']; ?>" ><?php echo $row['personen_angaben']; ?></textarea>
                                  </div>
                              </div>


                                <br>  
                             <div class="row">
                                  <label class="col-sm-5 control-label"><p style="color:red"><font size="2">Sind Sie (bzw. jemand in Ihrem Haushalt) momentan in &auml;rztliche Behandlung/Therapie(Physio) oder steht Ihnen eine Behandlung bevor?</font></p></label>
                                  <div class="col-sm-5">
                                      <input class="form-control"  type="text" id="fr_1<?php echo $row['t_id']; ?>" value="<?php echo $row['fr_1']; ?>" >
                                  </div>
                              </div>
                              <br>
                              <div class="row">
                                  <label class="col-sm-5 control-label"><p style="color:red"><font size="2">Zusatzfrage wenn JA- Dies k&ouml;nnte Auswirkungen auf Ihren Wechsel haben. Deshalb, welcher Art ist denn diese Behandlung?</font></p></label>
                                  <div class="col-sm-5">
                                      <input class="form-control"  type="text" id="fr_2<?php echo $row['t_id']; ?>" value="<?php echo $row['fr_2']; ?>">
                                  </div>
                              </div>
                              <br>
                            <div class="row">
                                  <label class="col-sm-5 control-label"><p style="color:red"><font size="2">Nehmen Sie (bzw. jemand in Ihrem Haushalt) regelm&auml;ssig Medikamente?</font></p></label>
                                  <div class="col-sm-5">
                                      <input class="form-control"  type="text" id="z_fr_2<?php echo $row['t_id']; ?>" value="<?php echo $row['z_fr_2']; ?>">
                                  </div>
                              </div>

                            <div class="row">
                                  <label class="col-sm-5 control-label"><font size="2"><p style="color:red">Zusatzfrage wenn JA- Was sind das f&uuml;r Medikamente oder f&uuml;r was m&uuml;ssen Sie diese nehmen?</p></font></label>
                                  <div class="col-sm-5">
                                      <input class="form-control"  id="fr_3<?php echo $row['t_id']; ?>" value="<?php echo $row['fr_3']; ?>" >
                                  </div>
                              </div>

                            <div class="row">
                                  <label class="col-sm-5 control-label"><font size="2"><p style="color:red">Hatten Sie (bzw. jemand an Ihrem Haushalt) in den letzten 10 Jahren eine Operation oder steht eine bevor?</p></font></label>
                                  <div class="col-sm-5">
                                      <input class="form-control"  type="text" id="z_fr_3<?php echo $row['t_id']; ?>" value="<?php echo $row['z_fr_3']; ?>">
                                  </div>
                              </div>

                             <div class="row">
                                  <label class="col-sm-5 control-label"><font size="2"><p style="color:red">Was f&uuml;r eine Operation wann /ist das?</p></font></label>
                                  <div class="col-sm-5">
                                      <input class="form-control"  type="text" id="fr_4<?php echo $row['t_id']; ?>" value="<?php echo $row['fr_4']; ?>" >
                                  </div>
                              </div>

                             <div class="row">
                                  <label class="col-sm-5 control-label"><font size="2"><p style="color:red">Wenn die Operation in der Vergangenheit war: beziehen Sie nun eine IV-Rente oder ist dies folgenlos geheilt?</p></font></label>
                                  <div class="col-sm-5">
                                      <input class="form-control"  type="text" id="z_fr_4<?php echo $row['t_id']; ?>" value="<?php echo $row['z_fr_4']; ?>" >
                                  </div>
                              </div>

                            <div class="row">
                                  <label class="col-sm-5 control-label"><font size="2"><p style="color:red">Falls 1 Person krank ist - K&ouml;nnen Sie sich vorstellen, einen Teil Ihrer Familie zu versichern?</p></font></label>
                                  <div class="col-sm-5">
                                      <input class="form-control"  type="text" id="fr_5<?php echo $row['t_id']; ?>" value="<?php echo $row['fr_5']; ?>"  >
                                  </div>
                              </div>

                            <div class="row">
                                  <label class="col-sm-5 control-label"><font size="2">Status</font></label>
                                  <div class="col-sm-5">
                                      <input class="form-control"  type="text" id="fr_6<?php echo $row['t_id']; ?>" value="<?php echo $row['status']; ?>">
                                  </div>
                              </div>





                            <div class="row">
                             <label class="col-sm-4 control-label"> </label>
                             <div class="col-sm-3">

                            </div>

                            </div>   
                          </form>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="button"  onclick="updatedata('<?php echo $row['t_id'] ?>')" data-dismiss="modal"  class="btn btn-primary">Update</button>

  </div>
</div>

和这个jquery ajax

function updatedata(str){

var t_id = str;
var anrede = $('#anrede'+str).val();
var vorname = $('#vorname'+str).val();
var nachname = $('#nachname'+str).val();
var strasse = $('#strasse'+str).val();
var plz = $('#plz'+str).val();
var ort = $('#ort'+str).val();
var krankenkasse = $('#krankenkasse'+str).val();
var seit = $('#seit'+str).val();
var personen = $('#personen'+str).val();
var telefon = $('#telefon'+str).val();
var termin = $('#termin'+str).val();
var time = $('#time'+str).val();
var vermittler = $('#vermittler'+str).val();
var coment = $('#coment'+str).val();
var feedback = $('#feedback'+str).val();
var s = new Array();
    $("input:checked").each(function() {
       s.push($(this).val());
    });


var positiv = $('#positiv'+str).val();
var personen_angaben = $('#personen_angaben'+str).val();
var fr_1 = $('#fr_1'+str).val();
var fr_2 = $('#fr_2'+str).val();
var z_fr_2 = $('#z_fr_2'+str).val();
var fr_3 = $('#fr_3'+str).val();
var z_fr_3 = $('#z_fr_3'+str).val();
var fr_4 = $('#fr_4'+str).val();
var z_fr_4 = $('#z_fr_4'+str).val();
var fr_5 = $('#fr_5'+str).val();
var fr_6 = $('#fr_6'+str).val();

var datas="anrede="+anrede+"&vorname="+vorname+"&nachname="+nachname+"&strasse="+strasse+"&plz="+plz+"&ort="+ort+"&krankenkasse="+krankenkasse+"&seit="+seit+"&personen="+personen+"&telefon="+telefon+"&termin="+termin+"&time="+time+"&vermittler="+vermittler+"&coment="+coment+"&feedback="+feedback+"&s[]="+s+"&positiv="+positiv+"&personen_angaben="+personen_angaben+"&fr_1="+fr_1+"&fr_2="+fr_2+"&z_fr_2="+z_fr_2+"&fr_3="+fr_3+"&z_fr_3="+z_fr_3+"&fr_4="+fr_4+"&z_fr_4="+z_fr_4+"&fr_5="+fr_5+"&fr_6="+fr_6;                                                                        
  alert(datas);
$.ajax({
   type: "POST",
   url: "updatedata.php?t_id="+t_id,
   data: datas,
   cache: false
}).done(function( data ) {
  $('#info').html(data);
    viewdata();
});
}

这就是php数组

$arrAstatus = array("Wollte k T","Kunde Ni err.","St .Gef","N.Zuhaus","Offen","Negativ","MJV","Behan.","Abgesagt","Positiv");

现在,当我提交表单时,它会更新所有未选中的复选框。

我怎么能这样做只更新已选中的复选框,并在刷新后向我显示已选中

2 个答案:

答案 0 :(得分:0)

问题:

  1. 您正在创建复选框ID作为数组语法s[]。我相信你会将它与name属性混淆。将此更改为id='s[]'name='s[]'
  2. <font>不再受支持。此外,该元素的开始和结束标记不正确。
  3. 你的ajax在哪里?你的form在哪里?你的提交按钮在哪里?如果没有这些信息,您可以假设这是一个表单,并且在提交表单时会触发您的jquery函数。如果是这种情况,那么您需要具有AJAX将用于发送请求的URL的action属性。
  4. <强>要点:

    请更具描述性,正确显示您的HTML,PHP,jQuery,以便我们能够为您提供最佳帮助。为了解决您的问题,您需要发送一个AJAX请求。了解如何做到这一点,在您的代码中实现它,然后我们可以从那里继续。

答案 1 :(得分:0)

看来你需要紧急帮助。我看到的主要问题是您错过了name元素中的form属性。您需要使用name属性,以便在jQuery中收集数据时可以使用.serialize()函数。我会以一种我认为你有效的方式重做你的代码。我将删除所有引导程序和非相关代码,以便您可以看到我在做什么:

我假设您的数据来自您的数据库。我将使用一个名为$data的数组来模仿该结果集:

//your database data
$data = array(
    array(
        "t_id" => 1,
        "username" => "john",
        "Astatus" => "Wollte k T,Kunde Ni err.,N.Zuhaus"
    )
);

//your comparison data
$arrAstatus = array("Wollte k T", "Kunde Ni err.", "St .Gef", "N.Zuhaus", "Offen", "Negativ", "MJV", "Behan.", "Abgesagt", "Positiv");

接下来,我假设您将每个结果迭代为$row并创建forms。我删除了你的类/ ids,因为我们不需要它们用于此目的。相反,我在您的输入中放置了所需的name属性:

<?php foreach ($data as $row) { ?>
        <form>
            <input type="text" name="username" value="<?php echo $row['username']; ?>" class="form-control">
            <input type="text" name="t_id" value="<?php echo $row['t_id']; ?>" class="form-control">

            <?php

            $iAstatus = explode(",", $row['Astatus']);
            foreach ($arrAstatus as $aAstatus) {
                if (in_array($aAstatus, $iAstatus))
                    echo "<label><input type='checkbox' name='s[]' value='$aAstatus'  checked>$aAstatus</label><br>";
                else
                    echo "<label><input type='checkbox' name='s[]' value='$aAstatus'>$aAstatus</label><br>";
            }
            ?>
            <input type="submit">
        </form>
<?php } ?>

最后,当使用.submit()函数提交表单时,应该触发您的jQuery。提交后,使用.serialize()获取具有name属性的所有输入并通过AJAX发送:

$('form').submit(function () {

    var datas = $(this).serialize();

    $.ajax({
        type: "POST",
        url: "submit.php",
        data: datas,
        cache: false
    }).done(function (data) {
        console.log(data);
    });
    return false;
});

希望这有帮助。