Hướng dẫn php radio button checked

Radio button là một công cụ giúp người dùng chọn một tùy chọn từ một nhóm các tùy chọn cùng loại.

Ví dụ 1: Tùy chọn giới tính

  • Nam
  • Nữ

Ví dụ 2: Tùy chọn hình thức thanh toán

  • Thanh toán tại nhà
  • Thanh toán Online

CẤU TRÚC HTML XÂY DỰNG RADIO BUTTON


Chú ý: Cách tùy chọn trong cùng một loại được khai báo cùng tên (name)

XÉT GIÁ TRỊ MẶC ĐỊNH CHO RADIO BUTTON

  • Khi để người dùng chọn một trong nhiều tùy chọn thì hệ thống có thể xét giá trị mặc định tùy chọn được lựa chọn ngay từ đầu. Đây có thể là các tùy chọn mà số lượng người dùng áp dụng nó nhiều nhất nhằm tránh mất thời gian của người dùng.
  • Để xét giá trị mặc định ta sử dụng  thuộc tính checked trong phần html của radio button

Ví dụ: Xét mặc định giới tính là Nam

 
  • Nhóm radio button có giá trị mặc định
  • Nhóm radio button không có giá trị mặc định

GHI NHỚ

  • Radio button dùng để lấy thông tin lựa chọn của người dùng từ một nhóm các lựa chọn cho trước
  • Các lựa chọn cùng loại được đặt cùng tên
  • Giá trị của radio button được xác định bằng value của radio được lựa chọn
  • Nếu không có bất kỳ lựa chọn nào dữ liệu của radio button không được đẩy lên server

Url Link

http://hocweb123.com/nhan-du-lieu-form-tu-radio-button.html

I am stuck on the issue. I have two radio buttons. One is a Buyer and the second is the Seller.

 Buyer
 Seller

The Buyer radio button is already checked.

Now What I am doing is, I have the below code, and if the $account_type==2 then I have to select the seller radio button and unchecked the buyer.

I tried the below code

 if(isset($_SESSION['error'])){
    $error = $_SESSION['error'];
    $account_type = $_SESSION['account_type'];
    unset($_SESSION['account_type']);
    unset($_SESSION['error']);
}
 
  Buyer
  checked="checked" > Seller

Jonas

113k94 gold badges299 silver badges371 bronze badges

asked Apr 9, 2021 at 17:51

Hướng dẫn php radio button checked

user9437856user9437856

2,2121 gold badge28 silver badges65 bronze badges

2

 

 >

answered Apr 9, 2021 at 18:10

Hướng dẫn php radio button checked

3

Here's the correction with the added empty() check.


$checked = [];
$checked[0] = empty($account_type) || (isset($account_type) && $account_type == 1) ? 'checked' : '';
$checked[1] = isset($account_type) && $account_type == 2 ? 'checked' : '';



answered Apr 9, 2021 at 18:05

Hướng dẫn php radio button checked

8

I hope that I don't duplicate other's answers and my solution isn't unnecessary, but I used this code snippet:


  checked="checked" > Buyer
  checked="checked" > Seller

answered Apr 9, 2021 at 18:29

Hướng dẫn php radio button checked

RoniRoni

914 bronze badges

I found the answer: What I did, I am checking the $account_type is empty then add checked. So I am getting a buyer checked.

I don't know this is the best answer or not but it's solved my issue.

I tried

if(isset($_SESSION['error'])){
        $error = $_SESSION['error'];
        $account_type = $_SESSION['account_type'];
        unset($_SESSION['account_type']);
        unset($_SESSION['error']);
    }

 checked="checked" >Buyer

 checked="checked" >  Seller

answered Apr 9, 2021 at 19:05

Hướng dẫn php radio button checked

user9437856user9437856

2,2121 gold badge28 silver badges65 bronze badges

3

Not the answer you're looking for? Browse other questions tagged php session radio-button or ask your own question.