Hướng dẫn html select option set selected value php - html select option đặt giá trị đã chọn php

Một cách khác (như được sử dụng trong WordPress) liên kết với mã WordPress sử dụng khả năng tái sử dụng sẽ là:

    

Hàm được chọn () xác định nếu giá trị được chọn và đặt tùy chọn đã chọn.

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}

Hàm được chọn () lần lượt gọi hàm trợ giúp bên dưới để so sánh và xác định xem giá trị có được chọn hay không. Nó trả về 'được chọn = đã chọn' hoặc ''.

/**
 * Private helper function for checked, selected, disabled and readonly.
 *
 * Compares the first two arguments and if identical marks as $type
 *
 * @since 2.8.0
 * @access private
 *
 * @param mixed  $helper  One of the values to compare
 * @param mixed  $current (true) The other value to compare if not just true
 * @param bool   $echo    Whether to echo or just return the string
 * @param string $type    The type of checked|selected|disabled|readonly we are doing
 * @return string html attribute or empty string
 */
function __checked_selected_helper( $helper, $current, $echo, $type ) {
    if ( (string) $helper === (string) $current ) {
        $result = " $type='$type'";
    } else {
        $result = '';
    }

    if ( $echo ) {
        echo $result;
    }

    return $result;
}

Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu cách sử dụng phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 để tạo danh sách thả xuống và hộp danh sách và cách lấy các giá trị được chọn từ phần tử
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 trong PHP.
: in this tutorial, you will learn how to use the
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 element to create a drop-down list and a list box and how to get the selected values from the
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 element in PHP.

Giới thiệu nhanh về yếu tố

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 là một thành phần HTML cung cấp danh sách các tùy chọn. Điều sau đây cho thấy cách xác định phần tử
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 trong HTML:

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green">Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)

Phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 có hai thuộc tính quan trọng:

  • /**
     * Private helper function for checked, selected, disabled and readonly.
     *
     * Compares the first two arguments and if identical marks as $type
     *
     * @since 2.8.0
     * @access private
     *
     * @param mixed  $helper  One of the values to compare
     * @param mixed  $current (true) The other value to compare if not just true
     * @param bool   $echo    Whether to echo or just return the string
     * @param string $type    The type of checked|selected|disabled|readonly we are doing
     * @return string html attribute or empty string
     */
    function __checked_selected_helper( $helper, $current, $echo, $type ) {
        if ( (string) $helper === (string) $current ) {
            $result = " $type='$type'";
        } else {
            $result = '';
        }
    
        if ( $echo ) {
            echo $result;
        }
    
        return $result;
    }
    
    2 -
    /**
     * Private helper function for checked, selected, disabled and readonly.
     *
     * Compares the first two arguments and if identical marks as $type
     *
     * @since 2.8.0
     * @access private
     *
     * @param mixed  $helper  One of the values to compare
     * @param mixed  $current (true) The other value to compare if not just true
     * @param bool   $echo    Whether to echo or just return the string
     * @param string $type    The type of checked|selected|disabled|readonly we are doing
     * @return string html attribute or empty string
     */
    function __checked_selected_helper( $helper, $current, $echo, $type ) {
        if ( (string) $helper === (string) $current ) {
            $result = " $type='$type'";
        } else {
            $result = '';
        }
    
        if ( $echo ) {
            echo $result;
        }
    
        return $result;
    }
    
    2 liên kết phần tử
    /**
     * Outputs the html selected attribute.
     *
     * Compares the first two arguments and if identical marks as selected
     *
     * @since 1.0.0
     *
     * @param mixed $selected One of the values to compare
     * @param mixed $current  (true) The other value to compare if not just true
     * @param bool  $echo     Whether to echo or just return the string
     * @return string html attribute or empty string
     */
    function selected( $selected, $current = true, $echo = true ) {
        return __checked_selected_helper( $selected, $current, $echo, 'selected' );
    }
    
    7 với phần tử
    /**
     * Private helper function for checked, selected, disabled and readonly.
     *
     * Compares the first two arguments and if identical marks as $type
     *
     * @since 2.8.0
     * @access private
     *
     * @param mixed  $helper  One of the values to compare
     * @param mixed  $current (true) The other value to compare if not just true
     * @param bool   $echo    Whether to echo or just return the string
     * @param string $type    The type of checked|selected|disabled|readonly we are doing
     * @return string html attribute or empty string
     */
    function __checked_selected_helper( $helper, $current, $echo, $type ) {
        if ( (string) $helper === (string) $current ) {
            $result = " $type='$type'";
        } else {
            $result = '';
        }
    
        if ( $echo ) {
            echo $result;
        }
    
        return $result;
    }
    
    5
  • /**
     * Private helper function for checked, selected, disabled and readonly.
     *
     * Compares the first two arguments and if identical marks as $type
     *
     * @since 2.8.0
     * @access private
     *
     * @param mixed  $helper  One of the values to compare
     * @param mixed  $current (true) The other value to compare if not just true
     * @param bool   $echo    Whether to echo or just return the string
     * @param string $type    The type of checked|selected|disabled|readonly we are doing
     * @return string html attribute or empty string
     */
    function __checked_selected_helper( $helper, $current, $echo, $type ) {
        if ( (string) $helper === (string) $current ) {
            $result = " $type='$type'";
        } else {
            $result = '';
        }
    
        if ( $echo ) {
            echo $result;
        }
    
        return $result;
    }
    
    6 - Thuộc tính
    /**
     * Private helper function for checked, selected, disabled and readonly.
     *
     * Compares the first two arguments and if identical marks as $type
     *
     * @since 2.8.0
     * @access private
     *
     * @param mixed  $helper  One of the values to compare
     * @param mixed  $current (true) The other value to compare if not just true
     * @param bool   $echo    Whether to echo or just return the string
     * @param string $type    The type of checked|selected|disabled|readonly we are doing
     * @return string html attribute or empty string
     */
    function __checked_selected_helper( $helper, $current, $echo, $type ) {
        if ( (string) $helper === (string) $current ) {
            $result = " $type='$type'";
        } else {
            $result = '';
        }
    
        if ( $echo ) {
            echo $result;
        }
    
        return $result;
    }
    
    6 liên kết với giá trị cho một bản gửi biểu mẫu.

Phần tử

/**
 * Private helper function for checked, selected, disabled and readonly.
 *
 * Compares the first two arguments and if identical marks as $type
 *
 * @since 2.8.0
 * @access private
 *
 * @param mixed  $helper  One of the values to compare
 * @param mixed  $current (true) The other value to compare if not just true
 * @param bool   $echo    Whether to echo or just return the string
 * @param string $type    The type of checked|selected|disabled|readonly we are doing
 * @return string html attribute or empty string
 */
function __checked_selected_helper( $helper, $current, $echo, $type ) {
    if ( (string) $helper === (string) $current ) {
        $result = " $type='$type'";
    } else {
        $result = '';
    }

    if ( $echo ) {
        echo $result;
    }

    return $result;
}
8 được lồng bên trong phần tử
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 xác định một tùy chọn trong menu. Mỗi tùy chọn có thuộc tính

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green">Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
0. Dữ liệu lưu trữ thuộc tính

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green">Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
0 được gửi đến máy chủ khi được chọn.

Nếu một tùy chọn không có thuộc tính

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green">Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
0, thuộc tính

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green">Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
0 mặc định cho văn bản bên trong phần tử
/**
 * Private helper function for checked, selected, disabled and readonly.
 *
 * Compares the first two arguments and if identical marks as $type
 *
 * @since 2.8.0
 * @access private
 *
 * @param mixed  $helper  One of the values to compare
 * @param mixed  $current (true) The other value to compare if not just true
 * @param bool   $echo    Whether to echo or just return the string
 * @param string $type    The type of checked|selected|disabled|readonly we are doing
 * @return string html attribute or empty string
 */
function __checked_selected_helper( $helper, $current, $echo, $type ) {
    if ( (string) $helper === (string) $current ) {
        $result = " $type='$type'";
    } else {
        $result = '';
    }

    if ( $echo ) {
        echo $result;
    }

    return $result;
}
8.

Để chọn tùy chọn khi trang tải lần đầu tiên, bạn có thể thêm thuộc tính

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green">Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
5 vào phần tử
/**
 * Private helper function for checked, selected, disabled and readonly.
 *
 * Compares the first two arguments and if identical marks as $type
 *
 * @since 2.8.0
 * @access private
 *
 * @param mixed  $helper  One of the values to compare
 * @param mixed  $current (true) The other value to compare if not just true
 * @param bool   $echo    Whether to echo or just return the string
 * @param string $type    The type of checked|selected|disabled|readonly we are doing
 * @return string html attribute or empty string
 */
function __checked_selected_helper( $helper, $current, $echo, $type ) {
    if ( (string) $helper === (string) $current ) {
        $result = " $type='$type'";
    } else {
        $result = '';
    }

    if ( $echo ) {
        echo $result;
    }

    return $result;
}
8.

Ví dụ sau chọn tùy chọn

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green">Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
7 khi trang lần đầu tiên tải:

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)

Nhận giá trị đã chọn từ một phần tử

Chúng tôi sẽ tạo một biểu mẫu sử dụng phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7.

Đầu tiên, hãy tạo các thư mục và tệp sau:

├── css | └── style.css ├── inc | ├── footer.php | ├── get.php | ├── header.php | └── post.php └── index.php

Code language: JavaScript (javascript)

Thứ hai, đặt mã sau vào tệp tiêu đề.php:

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="css/style.css"> <title>PHP select optiontitle> head> <body class="center"> <main>

Code language: HTML, XML (xml)

Thứ ba, đặt mã sau vào tệp footer.php:

main> body> html>

Code language: HTML, XML (xml)

Thứ tư, thêm mã sau vào tệp get.php để tạo biểu mẫu có một phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 bằng nút gửi:

<form action="" method="post"> <div> <label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select> div> <div> <button type="submit">Selectbutton> div> form>

Code language: HTML, XML (xml)

Biểu mẫu sử dụng phương thức

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
0 để gửi dữ liệu cho máy chủ web.

Cuối cùng, thêm mã sau vào tệp Post.php:

$color = filter_input(INPUT_POST, 'color', FILTER_SANITIZE_STRING); ?> if ($color) : ?> <p>You selected <span style="color:"> echo $color ?>span>p> <p><a href="index.php">Back to the forma>p> else : ?> <p>You did not select any colorp> endif ?>

Code language: HTML, XML (xml)

Để có được giá trị đã chọn của phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7, bạn sẽ sử dụng biến siêu global

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
2 nếu phương thức biểu mẫu là

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
0 và

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
4 nếu phương thức biểu mẫu là

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
5.

Ngoài ra, bạn có thể sử dụng hàm Filter_Input () để vệ sinh giá trị đã chọn.

Nếu bạn chọn tùy chọn đầu tiên của phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7, giá trị đã chọn sẽ trống. Mặt khác, giá trị được chọn là màu đỏ, xanh lá cây hoặc xanh dương.

Chọn với nhiều tùy chọn

Để bật nhiều lựa chọn, bạn thêm thuộc tính

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
7 vào phần tử
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7:

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
0

Khi bạn chọn nhiều tùy chọn của phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 và gửi biểu mẫu, tên sẽ chứa nhiều giá trị thay vì một giá trị duy nhất. Để nhận được nhiều giá trị đã chọn, bạn thêm dấu ngoặc vuông (

├── css | └── style.css ├── inc | ├── footer.php | ├── get.php | ├── header.php | └── post.php └── index.php

Code language: JavaScript (javascript)
0 sau tên của phần tử
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7.

Hãy cùng xem một ví dụ về việc sử dụng phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 với nhiều lựa chọn.

Đầu tiên, hãy tạo các thư mục và tệp sau:

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
1

Thứ hai, đặt mã sau vào tệp tiêu đề.php:

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
2

Thứ ba, đặt mã sau vào tệp footer.php:

main> body> html>

Code language: HTML, XML (xml)

Thứ tư, thêm mã sau vào tệp get.php để tạo biểu mẫu có một phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 bằng nút gửi:

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
4

Nếu yêu cầu HTTP là GET, tệp index.php sẽ hiển thị một biểu mẫu từ tệp get.php. Khi biểu mẫu được gửi, tệp Post.php sẽ xử lý việc gửi biểu mẫu.

Thứ năm, tạo một biểu mẫu chứa phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 với thuộc tính

<label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

Code language: HTML, XML (xml)
7 trong tệp

├── css | └── style.css ├── inc | ├── footer.php | ├── get.php | ├── header.php | └── post.php └── index.php

Code language: JavaScript (javascript)
8. Tên của phần tử
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
7 có khung hình vuông mở và đóng

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="css/style.css"> <title>PHP select optiontitle> head> <body class="center"> <main>

Code language: HTML, XML (xml)
0 để PHP có thể tạo một mảng giữ các giá trị chọn.

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
5

Cuối cùng, xử lý việc gửi biểu mẫu trong tệp

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="css/style.css"> <title>PHP select optiontitle> head> <body class="center"> <main>

Code language: HTML, XML (xml)
1:

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  (true) The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected( $selected, $current = true, $echo = true ) {
    return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
6

Tệp Post.php sử dụng hàm

html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="css/style.css"> <title>PHP select optiontitle> head> <body class="center"> <main>

Code language: HTML, XML (xml)
2 để lấy các màu được chọn dưới dạng mảng. Nếu bạn chọn một hoặc nhiều màu, tệp Post.php sẽ hiển thị chúng.

Bản tóm tắt

  • Sử dụng phần tử
    /**
     * Outputs the html selected attribute.
     *
     * Compares the first two arguments and if identical marks as selected
     *
     * @since 1.0.0
     *
     * @param mixed $selected One of the values to compare
     * @param mixed $current  (true) The other value to compare if not just true
     * @param bool  $echo     Whether to echo or just return the string
     * @return string html attribute or empty string
     */
    function selected( $selected, $current = true, $echo = true ) {
        return __checked_selected_helper( $selected, $current, $echo, 'selected' );
    }
    
    7 để tạo danh sách thả xuống.
  • Sử dụng thuộc tính

    <label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

    Code language: HTML, XML (xml)
    7 để tạo danh sách cho phép nhiều lựa chọn.
  • Sử dụng

    <label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

    Code language: HTML, XML (xml)
    2 để lấy giá trị đã chọn của phần tử chọn nếu phương thức biểu mẫu là

    <label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

    Code language: HTML, XML (xml)
    0 (hoặc

    <label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

    Code language: HTML, XML (xml)
    4 nếu phương thức biểu mẫu là

    <label for="color">Background Color:label> <select name="color" id="color"> <option value="">--- Choose a color ---option> <option value="red">Redoption> <option value="green" selected>Greenoption> <option value="blue">Blueoption> select>

    Code language: HTML, XML (xml)
    5).
  • Thêm dấu ngoặc vuông (________ 60) sau tên của phần tử
    /**
     * Outputs the html selected attribute.
     *
     * Compares the first two arguments and if identical marks as selected
     *
     * @since 1.0.0
     *
     * @param mixed $selected One of the values to compare
     * @param mixed $current  (true) The other value to compare if not just true
     * @param bool  $echo     Whether to echo or just return the string
     * @return string html attribute or empty string
     */
    function selected( $selected, $current = true, $echo = true ) {
        return __checked_selected_helper( $selected, $current, $echo, 'selected' );
    }
    
    7 để nhận nhiều giá trị được chọn.

Bạn có thấy hướng dẫn này hữu ích không?

Làm thế nào chúng ta có thể nhận được giá trị của tùy chọn đã chọn trong hộp chọn bằng PHP?

Để có được giá trị đã chọn của phần tử, bạn sử dụng biến SuperGlobal $ _POST nếu phương thức biểu mẫu là Post và $ _Get nếu phương thức biểu mẫu được nhận. Ngoài ra, bạn có thể sử dụng hàm Filter_Input () để vệ sinh giá trị đã chọn.use the $_POST superglobal variable if the form method is POST and $_GET if the form method is GET . Alternatively, you can use the filter_input() function to sanitize the selected value.

Làm thế nào có thể đặt giá trị đã chọn của thả xuống trong HTML?

Thẻ chọn trong HTML được sử dụng để tạo danh sách các tùy chọn thả xuống có thể được chọn.Thẻ tùy chọn chứa giá trị sẽ được sử dụng khi chọn.Giá trị mặc định của phần tử chọn có thể được đặt bằng cách sử dụng thuộc tính 'đã chọn' trên tùy chọn yêu cầu.The default value of the select element can be set by using the 'selected' attribute on the required option.

Làm cách nào để thay đổi các tùy chọn đã chọn trong HTML?

Để thay đổi tùy chọn đã chọn trong phần tử HTML, chúng tôi phải thay đổi thuộc tính giá trị trên phần tử hoặc thuộc tính đã chọn trên phần tử chúng tôi muốn được chọn.change the value property on the