dpbe81245
2017-01-08 09:45
浏览 232
已采纳

下拉框中选项选择属性的文本

I need to send an email notification of fields in a submitted form.

My question as follows is regarding the HTML select tag in the form.

<select id="country_code" name="country_code">
    <option value="" selected disabled>Country</option>
    <option value="AR">Argentina</option>
    <option value="BR">Brazil</option>
    <option value="CG">Congo</option>
    <option value="ZW">Zimbabwe</option>
</select>

Below I get the country code of a selected option:

$country_code = $_POST["country_code"];

How to get using PHP the text of the selected value in option tag (i.e. the country name)?

Note: I can't use MySQL, or JavaScript, and I can't change the select box values.

图片转代码服务由CSDN问答提供 功能建议

我需要以提交的形式发送字段的电子邮件通知。

我的问题如下:表格中的HTML选择标记。

 &lt; select id =“country_code”name =“country_code”&gt; 
&lt; option value  =“”已选中已停用&gt;国家&lt; /选项&gt; 
&lt;选项值=“AR”&gt;阿根廷&lt; /选项&gt; 
&lt;选项值=“BR”&gt;巴西&lt; /选项&gt; 
&lt;选项 value =“CG”&gt;刚果&lt; /选项&gt; 
&lt;选项值=“ZW”&gt;津巴布韦&lt; /选项&gt; 
&lt; / select&gt; 
   
 
 <  p>下面我得到所选选项的国家/地区代码: 
 
 
  $ country_code = $ _POST [“country_code”]; 
   
  
 

如何使用PHP在选项标签中选择值的文本(即国家名称)?

注意: 我不能使用MySQL或JavaScript, 我无法更改选择框值。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doucong1992 2017-01-08 09:58
    已采纳

    HTML:

    <select id="country_code" name="country_code">
        <option value="" selected disabled>Country</option>
        <option value="AR">Argentina</option>
        <option value="BR">Brazil</option>
        <option value="CG">Congo</option>
        <option value="ZW">Zimbabwe</option>
    </select>
    

    PHP:

    <?php
    if(isset($_POST['formSubmit'])){ 
    
    
    
        $countrySelection = $_POST['country_code'];
    
        $countrycode_list = array('AF', 'AX', 'AL', 'DZ', 'AS', 'AD', 'AO', 
        'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 
        'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BQ', 'BA', 'BW', 'BV', 
        'BR', 'IO', 'BN', 'BG', 'BF', 'BI', 'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 
        'TD', 'CL', 'CN', 'CX', 'CC', 'CO', 'KM', 'CG', 'CD', 'CK', 'CR', 'CI', 
        'HR', 'CU', 'CW', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'DO', 'EC', 'EG', 'SV', 
        'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'GF', 'PF', 'TF', 
        'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 
        'GG', 'GN', 'GW', 'GY', 'HT', 'HM', 'VA', 'HN', 'HK', 'HU', 'IS', 'IN', 
        'ID', 'IR', 'IQ', 'IE', 'IM', 'IL', 'IT', 'JM', 'JP', 'JE', 'JO', 'KZ', 
        'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY',
        'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH',
        'MQ', 'MR', 'MU', 'YT', 'MX', 'FM', 'MD', 'MC', 'MN', 'ME', 'MS', 'MA', 
        'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'NC', 'NZ', 'NI', 'NE', 'NG', 'NU', 
        'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 'PE', 'PH', 
        'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO', 'RU', 'RW', 'BL', 'SH', 'KN', 
        'LC', 'MF', 'PM', 'VC', 'WS', 'SM', 'ST', 'SA', 'SN', 'RS', 'SC', 'SL', 
        'SG', 'SX', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'SS', 'ES', 'LK', 'SD', 
        'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL', 'TG', 
        'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 
        'US', 'UM', 'UY', 'UZ', 'VU', 'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 
        'ZM', 'ZW');
    
        $ISO3166 = NULL;
        //Loop all country codes
        foreach ($countrycode_list as $countrycode) {
            $locale_cc = Locale::getDisplayRegion('-' . $countrycode, 
            $countrySelection);
    
            if (strcasecmp($country_name, $locale_cc) == 0) {
                $ISO3166 = $countrycode;
                break;
            }
        }
    //return NULL if not found or country code
    return $ISO3166;
    }
    
    }
    ?>
    
    打赏 评论
  • droirqk4795 2017-01-08 10:13

    Change the values in JS when the form is submitted OR Change the value to:

    <option value="CG_Congo">Congo</option>
    

    and then split it using explode or split

    $location = explode("_", $_POST["country_code"]);
    

    OR Save it in a DB and query OR Save an array in your code with the translations:

    $translations = ['CG' => "Congo", etc];
    

    Would really not recommend the last one however. I don't see why wouldn't you change the option value as you still get the country code but the country name as an addition.

    Edit: Found a JSON country name list online: http://country.io/names.json, You can do something like this:

    $country_code_to_name = json_decode(file_get_contents("http://country.io/names.json"), true);
    echo $country_code_to_name[$_POST["country_code"]];
    
    打赏 评论

相关推荐 更多相似问题