多选复选框以显示表记录

时间:2016-11-22 18:42:54

标签: php jquery html mysql

我想通过选择多个公司名称使用多个选择复选框来过滤表格,表格将显示与所选复选框相关的记录。它只能选择一个复选框并仅显示一个相关记录,但不能选择多个复选框。

<form name="frmSearch" id="frmSearch" method="post" action="">

  <label>Company Name&nbsp;</label>
  <select id="multiple-checkboxes" multiple="multiple" name="COMPANYNAME">
    <?php
        $query = mysqli_query($conn_connection, "SELECT * FROM sl_iv GROUP by COMPANYNAME");

        while ($row = mysqli_fetch_assoc($query)) {
            echo "<option value='".$row["COMPANYNAME"]."'".($row["COMPANYNAME"]==$_POST["COMPANYNAME"] ? " selected" : "").">".$row["COMPANYNAME"]."</option>";
        }
    ?>
  </select>
  <br></br>

  <button type="submit" id="submit" class="btn btn-info btn-sm"><span class="glyphicon glyphicon-search"></span> Search</button>
  <a href="cust_due_list.php">
    <button type="button" class="btn btn-success btn-sm"><span class="glyphicon glyphicon-refresh"></span> RESET</button>
  </a>

  <br></br>

  <table id="table">
    <center>
      <thead>
        <tr class="item-row">
          <th width="15%" style="text-align:center"><span>Doc No.</span></th>
          <th width="10%" style="text-align:center"><span>Due</span></th>
          <th width="5%" style="text-align:center"><span>Age</span></th>
          <th width="20%" style="text-align:center"><span>Customer Name</span></th>
          <th width="10%" style="text-align:center"><span>Ammount</span></th>
          <th width="10%" style="text-align:center"><span>Payment</span></th>
          <th width="10%" style="text-align:center"><span>OutStanding</span></th>
        </tr>
      </thead>
    </center>

    <tbody>
      <?php
                    if(isset ($_POST['COMPANYNAME']))
                    {
                        $COMPANYNAME = $_POST['COMPANYNAME'];
                        $fetch = "SELECT sl_iv.DOCDATE, ar_iv.DUEDATE, payment_terms.terms, sl_iv.DOCNO, sl_iv.COMPANYNAME, ar_iv.DOCAMT, ar_iv.PAYMENTAMT FROM `sl_iv` Inner Join `ar_iv` On ar_iv.DOCNO = sl_iv.DOCNO Inner Join `payment_terms` On ar_iv.TERMS = payment_terms.id WHERE sl_iv.COMPANYNAME = '".$COMPANYNAME."' or sl_iv.DOCDATE <= '".$from."'";
                        $result = mysqli_query($conn_connection,$fetch)or die("MySQL error: " . mysqli_error($conn_connection) . "<hr>\nQuery: $fetch");
                    }
                    if (mysqli_num_rows($result) > 0) {
                        // output data of each row
                        while($row = mysqli_fetch_assoc($result)) {

                        $docamt = $row['DOCAMT']; 

                    ?>
        <tr class="item-row">
          <td>
            <input type="text" style="text-align:center; font-size:15px" class="form-control input-sm DocNo" id=DocNo0 " name="DocNo " value="<?php echo htmlspecialchars($row[ 'DOCNO']);?>" readonly></td>
          <td>
            <input type="text" style="text-align:center; font-size:15px" class="form-control input-sm DueDate" id="DueDate0" name="DueDate" value="<?php echo htmlspecialchars($row['DUEDATE']);?>" readonly>
          </td>
          <td>
            <input type="text" style="text-align:center; font-size:15px" class="form-control input-sm DateAge" id="DateAge0" name="DateAge" value="<?php echo $dateage;?>" readonly>
          </td>
          <td>
            <input type="text" style="text-align:center; font-size:15px" class="form-control input-sm CompanyName" id="CompanyName0" name="CompanyName" value="<?php echo htmlspecialchars($row['COMPANYNAME']);?>" readonly>
          </td>
          <td>
            <input type="text" style="text-align:right; font-size:15px" class="form-control input-sm TotalAmt" id="TotalAmt0" name="TotalAmt" value="<?php echo htmlspecialchars($row['DOCAMT']);?>" readonly>
          </td>
          <td>
            <input type="text" style="text-align:right; font-size:15px" class="form-control input-sm payment" id="payment0" name="payment" value="<?php echo htmlspecialchars($row['PAYMENTAMT']);?>" readonly>
          </td>
          <td>
            <input type="text" style="text-align:right; font-size:15px" class="form-control input-sm Total_Outstanding" id="Total_Outstanding0" name="Total_Outstanding" value="<?php echo number_format((float)$outstanding, 2, '.', '');?>" readonly>
          </td>
        </tr>
        <?php
                        }
                    } else {
                        echo "0 results";
                    }
                    ?>

    </tbody>
  </table>
</form>



<script type="text/javascript">
  $(document).ready(function() {
    $('#multiple-checkboxes').multiselect();
  });

</script>

1 个答案:

答案 0 :(得分:0)

当您收到多选POST时,它会以数组形式出现,因此您应该使用&#34; IN&#34;您的mysql请求中的运算符而不是比较。 尝试更改代码的一部分:

if(isset ($_POST['COMPANYNAME'])) {
   $COMPANYNAME = $_POST['COMPANYNAME'];
   $COMPANYNAME = is_array($COMPANYNAME) ? $COMPANYNAME : [$COMPANYNAME];

   $companiesParam = '\''. join("', '", $COMPANYNAME) . '\'';
   $fetch = "SELECT sl_iv.DOCDATE, ar_iv.DUEDATE, payment_terms.terms, sl_iv.DOCNO, sl_iv.COMPANYNAME, ar_iv.DOCAMT, ar_iv.PAYMENTAMT FROM `sl_iv` Inner Join `ar_iv` On ar_iv.DOCNO = sl_iv.DOCNO Inner Join `payment_terms` On ar_iv.TERMS = payment_terms.id WHERE sl_iv.COMPANYNAME IN (".$companiesParam.") or sl_iv.DOCDATE <= '".$from."'";
   $result = mysqli_query($conn_connection,$fetch)or die("MySQL error: " . mysqli_error($conn_connection) . "<hr>\nQuery: $fetch");
}