html 表单到 php 数组

我试图将一个字符串作为用户输入的表单,并使用一组国家/地区检查它是否是一个国家/地区。我现在收到此错误:语法错误,意外的“”(T_ENCAPSED_AND_WHITESPACE),期待“-”或标识符(T_STRING)或变量(T_VARIABLE)或数字(T_NUM_STRING)


<html>

<head></head>

<body>

<h1> Welcome to the Country Searcher </h1>

<form method="get">

    <p><label for="guess">Input Guess</label>

    <input type="text" name="guess" id="guess"/></p>

    <input type="submit"/>

</form>


<?php


    $countries = array("Afghanistan", "Albania", "Algeria", "American Samoa", 

                "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", 

                "Argentina", "Armenia", "Aruba", "Australia", "Austria", 

                "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", 

                "Barbados", "Belarus", "Belgium", "Belize", "Benin", 

                "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", 

                "Botswana", "Bouvet Island", "Brazil", 

                "British Indian Ocean Territory", "Brunei Darussalam", 

                "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", 

                "Cameroon", "Canada", "Cape Verde", "Cayman Islands", 

                "Central African Republic", "Chad", "Chile", "China", 


    if( in_array("$_GET['guess']",  $countries)) {

        echo "Your country $_GET['guess'] is valid!";

    } else {

        echo "I'm sorry! You've entered an invalid string!";

    }

?>


</body>

</html>


缥缈止盈
浏览 135回答 3
3回答

手掌心

你可以通过使用使这更简单in_array()在标签上 soecify 一个方法也是一个好主意<form>。默认值可能是 GET,但如果您对其进行编码,那么您就是在自我记录您的代码。<html><head></head><body><h1> Welcome to the Country Searcher </h1><form method="get">&nbsp; &nbsp; <p><label for="guess">Input Guess</label>&nbsp; &nbsp; <input type="text" name="guess" id="guess"/></p>&nbsp; &nbsp; <input type="submit"/></form><?php&nbsp; &nbsp; $countries = array("Afghanistan", "Albania", "Algeria", "American Samoa",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Argentina", "Armenia", "Aruba", "Australia", "Austria",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Barbados", "Belarus", "Belgium", "Belize", "Benin",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Botswana", "Bouvet Island", "Brazil",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "British Indian Ocean Territory", "Brunei Darussalam",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Bulgaria", "Burkina Faso", "Burundi", "Cambodia",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Cameroon", "Canada", "Cape Verde", "Cayman Islands",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Central African Republic", "Chad", "Chile", "China",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "France Metropolitan", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard and Mc Donald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao, People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia, The Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia (Slovak Republic)", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon", "Sudan", "Suriname", "Svalbard and Jan Mayen Islands", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (U.S.)", "Wallis and Futuna Islands", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe");&nbsp; &nbsp; if( in_array($_GET['guess'],&nbsp; $countries) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Your country $_GET[guess] is valid!";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo "I'm sorry! You've entered an invalid string!";&nbsp; &nbsp; }?></body></html>

不负相思意

我终于想通了这个问题!我分配了一个变量GET并用这个变量代替了GET所有地方。这似乎解决了问题!

白衣非少年

$_GET是输入参数数组(来自 url)。您必须按名称取值:$_GET['guess']if($countries[$x]==$_GET['guess'])并使用逗号连接字符串:echo 'Your country ' . $_GET['guess'] . ' is valid!';
打开App,查看更多内容
随时随地看视频慕课网APP