Hiển thị các bài đăng có nhãn Checkbox Ajax. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Checkbox Ajax. Hiển thị tất cả bài đăng

Thứ Ba, 23 tháng 12, 2025

Checkbox Limit - chọn giới hạn trong checkbox

 


<!DOCTYPE html>

<html lang="vi">

<head>

  <meta charset="UTF-8">

  <title>Checkbox Dropdown - Show Value</title>

  <style>

    body {

      font-family: Arial, sans-serif;

      background: #f5f5f5;

      padding: 40px;

    }

    .container {

      width: 420px;

      margin: auto;

      background: #fff;

      padding: 20px;

      border-radius: 8px;

      box-shadow: 0 2px 8px rgba(0,0,0,.1);

    }

    .dropdown { position: relative; }

    .dropdown-btn {

      width: 100%;

      padding: 10px;

      border: 1px solid #ccc;

      border-radius: 6px;

      background: #fff;

      cursor: pointer;

      text-align: left;

    }

    .dropdown-menu {

      position: absolute;

      top: 45px;

      left: 0;

      width: 100%;

      background: #fff;

      border-radius: 8px;

      border: 1px solid #ddd;

      box-shadow: 0 4px 12px rgba(0,0,0,.15);

      padding: 12px;

      display: none;

      z-index: 10;

    }

    .dropdown-menu label {

      display: flex;

      align-items: center;

      margin-bottom: 8px;

      cursor: pointer;

    }

    .dropdown-menu input { margin-right: 8px; }

    .disabled { color: #aaa; cursor: not-allowed; }

    .selected-info {

      font-size: 13px;

      color: #666;

      margin-bottom: 8px;

    }

    .tags { margin-top: 12px; }

    .tag {

      display: inline-block;

      background: #ffe7d9;

      color: #ff6a00;

      padding: 4px 8px;

      border-radius: 6px;

      font-size: 13px;

      margin-right: 6px;

      margin-bottom: 6px;

    }

  </style>

</head>

<body>


<div class="container">

  <h3>Business Type</h3>


  <form method="POST" action="submit.php">


    <!-- INPUT GỬI BACKEND -->

    <input type="hidden" name="searchname" id="searchname">


    <div class="dropdown">

      <button type="button" class="dropdown-btn" id="dropdownBtn">

        Chọn loại hình kinh doanh

      </button>


      <div class="dropdown-menu" id="dropdownMenu">

        <div class="selected-info">

          Selected: <span id="count">0</span>/3

        </div>


        <label><input type="checkbox" value="Import and Export">Import and Export</label>

        <label><input type="checkbox" value="Retail & Agency">Retail & Agency</label>

        <label><input type="checkbox" value="Manufacturer">Manufacturer</label>

        <label><input type="checkbox" value="Wholesale">Wholesale</label>

        <label><input type="checkbox" value="Service Provider">Service Provider</label>

      </div>

    </div>


    <div class="tags" id="tags"></div>


    <button style="margin-top:15px;width:100%">Submit</button>

  </form>

</div>


<script>

  const dropdownBtn = document.getElementById("dropdownBtn");

  const dropdownMenu = document.getElementById("dropdownMenu");

  const checkboxes = dropdownMenu.querySelectorAll("input[type=checkbox]");

  const countEl = document.getElementById("count");

  const tagsEl = document.getElementById("tags");

  const hiddenInput = document.getElementById("searchname");

  const MAX = 3;

  const placeholder = "Chọn loại hình kinh doanh";


  dropdownBtn.onclick = () => {

    dropdownMenu.style.display =

      dropdownMenu.style.display === "block" ? "none" : "block";

  };


  document.addEventListener("click", e => {

    if (!e.target.closest(".dropdown")) {

      dropdownMenu.style.display = "none";

    }

  });


  checkboxes.forEach(cb => {

    cb.addEventListener("change", () => {

      const checked = [...checkboxes].filter(c => c.checked);

      if (checked.length > MAX) {

        cb.checked = false;

        return;

      }

      updateUI();

    });

  });


  function updateUI() {

    const checked = [...checkboxes].filter(c => c.checked);

    countEl.textContent = checked.length;


    // Disable khi đủ 3

    checkboxes.forEach(cb => {

      const label = cb.parentElement;

      if (!cb.checked && checked.length === MAX) {

        cb.disabled = true;

        label.classList.add("disabled");

      } else {

        cb.disabled = false;

        label.classList.remove("disabled");

      }

    });


    const values = checked.map(c => c.value);


    // 👉 HIỆN TRONG BUTTON

    dropdownBtn.textContent =

      values.length ? values.join(", ") : placeholder;


    // 👉 GỬI FORM

    hiddenInput.value = values.join(",");


    // // Tags

    // tagsEl.innerHTML = values

    //   .map(v => `<span class="tag">${v}</span>`)

    //   .join("");

  }

</script>


</body>

</html>


Thứ Ba, 9 tháng 3, 2021

HƯỚNG DẪN LẤY DỮ LIỆU TỪ NHIỀU CHECKBOX BẰNG AJAX /JQUERY VÀ PHP

Link: https://ttsvn.net/huong-dan-lay-du-lieu-tu-nhieu-checkbox-bang-ajax-jquery-va-php 

Ở bài hướng dẫn này , mình sẽ hướng dẫn các bạn cách lấy dữ liệu từ nhiều checkbox bằng ajax kết hợp với PHP. Nói thêm về Ajax, nó là một công nghệ được google sáng tạo ra dựa trên php và jQuery, là công nghệ lấy dữ liệu từ máy chủ mà không cần load lại trang web, cho phép chúng ta load cái mới mà không cần tải lại cái cũ sẽ đỡ mất thời gian hơn nên duyệt web sẽ nhanh hơn.

– Bước 1: Các cần phải tải thư viện jQuey.min.js , hoặc link trực tiếp như mình.

– Bước 2: Tạo một file index.php trong localhost với nội dung như sau:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>

<script>
jQuery(document).ready(function( ){
jQuery("input[name='checkbox[]']").click(function(){
var stringV = '';
// su dung vong lap de them gia tri checkbox vao chuoi....
jQuery("input[name='checkbox[]']:checked").each(function(){
stringV = stringV + ',' + jQuery(this).val();});
if(stringV.length > 1){
stringV = stringV.substring(1);
}
//ket thuc lap-----------&gt; su dung ajax gui den server.
//alert(stringV);
jQuery.ajax({
url:"xuly.php",
type:"post",
data:"id="+ stringV,
async: true,
success: function(result){
jQuery(".results").html(result);
}
})
})
});
</script>
<head>
<body>
<input type="checkbox" name="checkbox[]" id="1" value="1">
<input type="checkbox" name="checkbox[]" id="2" value="2">
<input type="checkbox" name="checkbox[]" id="3" value="3">
<input type="checkbox" name="checkbox[]" id="4" value="4">
<input type="checkbox" name="checkbox[]" id="5" value="5">
<input type="checkbox" name="checkbox[]" id="6" value="6">
<div class="results"></div> </body>
</html>

– Bước 3: Tạo một file xuly.php trong localhost với nội dung như sau:

<?php
$string= $_POST["id"]  ;
echo $string;
?>

 


Các bạn để ý : cặp thẻ này “<div class=”results”></div>” ở trong file index.php có tác dụng hiển thị giá trị trả về từ sever bằng cú pháp jQUery :

success: function(result){

jQuery(".results").html(result);

}