简体   繁体   English

选择多个复选框并使用提交按钮更新数据库

[英]Select multiple checkboxes and update the database using submit button

I have just finished small project on multiple checkboxes using ajax. 我刚刚使用ajax在多个复选框上完成了小型项目。 But now i want to use submit button for filter option. 但是现在我想使用提交按钮作为过滤器选项。 So now after multiple checkboxes are selected and after sumbit button are clicked then only it should change in the phone database. 因此,现在在选择了多个复选框并单击了sumbit按钮之后,只有它应该在电话数据库中更改。 Any help ? 有什么帮助吗? Thanks. 谢谢。 Here is my code: Index.php 这是我的代码:Index.php

<script>


 function makeTable(data){
    var tbl_body = "";
    $.each(data, function() {
      var tbl_row = "",
          currRecord = this;

      $.each(this, function(k , v) {
        if(k==='model'){
          v = "<a href='content.php?id=" + currRecord['id'] +"'>" + v + "</a>";
        } else if (k==='price'){
          v = "<span class='price'>" + v + "</span>";
        }
        tbl_row += "<td>"+v+"</td>";
      })
      tbl_body += "<tr>"+tbl_row+"</tr>";
    })

    return tbl_body;
  }

  function getPhoneFilterOptions(){
    var opts = [];
    $checkboxes.each(function(){
      if(this.checked){
        opts.push(this.id);
      }
    });

    return opts;
  }


  function updatePhones(opts){
    if(!opts || !opts.length){
      opts = allBrands;
    }

    $.ajax({
      type: "POST",
      url: "submit.php",
      dataType : 'json',
      cache: false,
      data: {filterOpts: opts},
      success: function(records){
        $('#phones tbody').html(makeTable(records));
        updatePrices();
      }
    });
  }

  function subsidyIsValid(){
    var amount1 = $("#amount1").val(),
        amount2 = $("#amount2").val(),
        regex = /^\d+$/,
        inputValid = false;

    if(regex.test(amount1) && regex.test(amount2)){
      var newTotal = Number(amount1) + Number(amount2)
      $("#total").text(newTotal);
      inputValid = true;
    }

    return inputValid
  }

  function updatePrices(){
    var subsidyTotal = Number($("#total").text());

    $(".price").each(function(){
      var origVal = Number($(this).text())
      $(this).text(origVal - subsidyTotal)
    })
  }

 $("#submitFilter").on("click", function(){
    var opts = getPhoneFilterOptions();
    updatePhones(opts);
        })

  $("#apply").on("click", function(){
    if(subsidyIsValid()){
      $(this).prop("disabled", true);
      $(this).next().prop("disabled", false);
      updatePrices();
    } else {
      alert("Subsidy invalid!")
    }
  });

  $("#remove").on("click", function(){
    $("#amount1").val("");
    $("#amount2").val("");
    $("#total").text("0");
    $(this).prop("disabled", true);
    $(this).prev().prop("disabled", false);

    $checkboxes.trigger("change");
  });

  var allBrands = [];
  $("input[type=checkbox]").each(function(){
    allBrands.push($(this)[0].id)
  })

  updatePhones();
  updatePrices();
</script> 

Submit.php Submit.php

<?php 
require 'Database.php';
#### TEMP SET NAMES FÜR UTF8 ###################################################
include 'Json.php';
  $opts = $_POST['filterOpts'];
  $tmp = array();
  foreach ($opts as $opt) {
        $tmp[] = '"'.$opt.'"';
  }
         $query = 
      'SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN ('.implode(",", $tmp).')';


  $result = mysql_query($query);
  $data   = array();
  while ($row = mysql_fetch_assoc($result)) {
  $data[] = $row;
  }

echo json_encode($data);
?>

Well first of all, remove this listener: 首先,删除此侦听器:

$checkboxes.on("change", function(){
    var opts = getPhoneFilterOptions();
    updatePhones(opts);
  });

You don't want to change data in table on checking of a checkbox but only after clicking submit button. 您不想在选中复选框时更改表中的数据,而仅在单击提交按钮之后才更改。 I don't see any "Submit" button other than "Apply" that seems to have to do with subsidue options more than with filtering, so add a 'submit' first (actually just a button): 除了“应用”外,我没有看到任何其他“提交”按钮,这似乎与过滤选项更多地与补贴选项有关,因此请首先添加“提交”(实际上只是一个按钮):

(...)
<div>
  <input type="checkbox" id="Nokia">
  <label for="Nokia">Nokia</label>
</div>
<button id="submitFilter">Filter</button>
(...)

Then throw what you had in checkbox listener to the onClick of submitFilter. 然后将您在复选框侦听器中拥有的内容扔到SubmitFilter的onClick上。

 $("#submitFilter").on("click", function(){
     var opts = getPhoneFilterOptions();
     updatePhones(opts);
 });

I updated the fiddle so it is more accurate to your needs. 我更新了小提琴,以便更准确地满足您的需求。 Check it here 在这里检查

EDIT: 编辑:

To open a new tab: 要打开新标签页:

var win = window.open(url, '_blank');
win.focus();

Where url is obviously an url you want to open. url显然是您要打开的URL。 You can also set it as form target and put the data you want to pass in a form, and make filter a submit. 您还可以将其设置为表单目标,并将要传递的数据放入表单中,然后将过滤器提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM