'I have two pages. on the first page is a select box, which has all the names of lots of countrys, when the user clicks a country and clicks send, it goes to the second page where the relevant flag is located. However, its not working, when i cant see whats wrong. I have spent alot of time on this and is annoying me so if anyone can help that would be good.
1st Page:
<?php
echo "<form action='test2.php' method='post'>
";
echo '<br>Actual Region: <img src="flags/png/' . $country . '.png">';
$countries = array(
' af ' =>' Afghanistan ' ,
' al ' =>' Albania ' ,
' dz ' =>' Algeria ' ,
' as ' =>' American Samoa ' ,
' ad ' =>' Andorra ' ,
' ao ' =>' Angola ' ,
' ai ' =>' Anguilla ' ,
' aq ' =>' Antarctica ' ,
' ag ' =>' Antigua and Barbuda ' ,
' ar ' =>' Argentina ' ,
' am ' =>' Armenia ' ,
' aw ' =>' Aruba ' ,
' au ' =>' Australia ' ,
' at ' =>' Austria ' ,
' az ' =>' Azerbaijan ' ,
' bs ' =>' Bahamas ' ,
' bh ' =>' Bahrain ' ,
' bd ' =>' Bangladesh ' ,
' bb ' =>' Barbados ' ,
' by ' =>' Belarus ' ,
' be ' =>' Belgium ' ,
' bz ' =>' Belize ' ,
' bj ' =>' Benin ' ,
' bm ' =>' Bermuda ' ,
' bt ' =>' Bhutan ' ,
' bo ' =>' Bolivia ' ,
' ba ' =>' Bosnia-Herzegovina ' ,
' bw ' =>' Botswana ' ,
' bv ' =>' Bouvet Island ' ,
' br ' =>' Brazil ' ,
' io ' =>' British Indian Ocean Territory ' ,
' bn ' =>' Brunei Darussalam ' ,
' bg ' =>' Bulgaria ' ,
' bf ' =>' Burkina Faso ' ,
' bi ' =>' Burundi ' ,
' kh ' =>' Cambodia ' ,
' cm ' =>' Cameroon ' ,
' ca ' =>' Canada ' ,
' cv ' =>' Cape Verde ' ,
' ky ' =>' Cayman Islands ' ,
' cf ' =>' Central African Republic ' ,
' td ' =>' Chad ' ,
' cl ' =>' Chile ' ,
' cn ' =>' China ' ,
' cx ' =>' Christmas Island ' ,
' cc ' =>' Cocos (Keeling) Islands ' ,
' co ' =>' Colombia ' ,
' km ' =>' Comoros ' ,
' cg ' =>' Congo ' ,
' cd ' =>' Congo, Dem. Republic ' ,
' ck ' =>' Cook Islands ' ,
' cr ' =>' Costa Rica ' ,
' hr ' =>' Croatia ' ,
' cu ' =>' Cuba ' ,
' cy ' =>' Cyprus ' ,
' cz ' =>' Czech Rep. ' ,
' dk ' =>' Denmark ' ,
' dj ' =>' Djibouti ' ,
' dm ' =>' Dominica ' ,
' do ' =>' Dominican Republic ' ,
' ec ' =>' Ecuador ' ,
' eg ' =>' Egypt ' ,
' sv ' =>' El Salvador ' ,
' gq ' =>' Equatorial Guinea ' ,
' er ' =>' Eritrea ' ,
' ee ' =>' Estonia ' ,
' et ' =>' Ethiopia ' ,
' eu.int ' =>' European Union ' ,
' fk ' =>' Falkland Islands ' ,
' fo ' =>' Faroe Islands ' ,
' fj ' =>' Fiji ' ,
' fi ' =>' Finland ' ,
' fr ' =>' France ' ,
' gf ' =>' French Guiana ' ,
' tf ' =>' French Southern Territories ' ,
' ga ' =>' Gabon ' ,
' gm ' =>' Gambia ' ,
' ge ' =>' Georgia ' ,
' de ' =>' Germany ' ,
' gh ' =>' Ghana ' ,
' gi ' =>' Gibraltar ' ,
' gb ' =>' Great Britain ' ,
' gr ' =>' Greece ' ,
' gl ' =>' Greenland ' ,
' gd ' =>' Grenada ' ,
' gp ' =>' Guadeloupe (French) ' ,
' gu ' =>' Guam (USA) ' ,
' gt ' =>' Guatemala ' ,
' gg ' =>' Guernsey ' ,
' gn ' =>' Guinea ' ,
' gw ' =>' Guinea Bissau ' ,
' gy ' =>' Guyana ' ,
' ht ' =>' Haiti ' ,
' hm ' =>' Heard Island and McDonald Islands ' ,
' hn ' =>' Honduras ' ,
' hk ' =>' Hong Kong ' ,
' hu ' =>' Hungary ' ,
' is ' =>' Iceland ' ,
' in ' =>' India ' ,
' id ' =>' Indonesia ' ,
' ir ' =>' Iran ' ,
' iq ' =>' Iraq ' ,
' ie ' =>' Ireland ' ,
' im ' =>' Isle of Man ' ,
' il ' =>' Israel ' ,
' it ' =>' Italy ' ,
' ci ' =>' Ivory Coast ' ,
' jm ' =>' Jamaica ' ,
' jp ' =>' Japan ' ,
' je ' =>' Jersey ' ,
' jo ' =>' Jordan ' ,
' kz ' =>' Kazakhstan ' ,
' ke ' =>' Kenya ' ,
' ki ' =>' Kiribati ' ,
' kp ' 开发者_StackOverflow社区 =>' Korea-North ' ,
' kr ' =>' Korea-South ' ,
' kw ' =>' Kuwait ' ,
' kg ' =>' Kyrgyzstan ' ,
' la ' =>' Laos ' ,
' lv ' =>' Latvia ' ,
' lb ' =>' Lebanon ' ,
' ls ' =>' Lesotho ' ,
' lr ' =>' Liberia ' ,
' ly ' =>' Libya ' ,
' li ' =>' Liechtenstein ' ,
' lt ' =>' Lithuania ' ,
' lu ' =>' Luxembourg ' ,
' mo ' =>' Macau ' ,
' mk ' =>' Macedonia ' ,
' mg ' =>' Madagascar ' ,
' mw ' =>' Malawi ' ,
' my ' =>' Malaysia ' ,
' mv ' =>' Maldives ' ,
' ml ' =>' Mali ' ,
' mt ' =>' Malta ' ,
' mh ' =>' Marshall Islands ' ,
' mq ' =>' Martinique (French) ' ,
' mr ' =>' Mauritania ' ,
' mu ' =>' Mauritius ' ,
' yt ' =>' Mayotte ' ,
' mx ' =>' Mexico ' ,
' fm ' =>' Micronesia ' ,
' md ' =>' Moldova ' ,
' mc ' =>' Monaco ' ,
' mn ' =>' Mongolia ' ,
' me ' =>' Montenegro ' ,
' ms ' =>' Montserrat ' ,
' ma ' =>' Morocco ' ,
' mz ' =>' Mozambique ' ,
' mm ' =>' Myanmar ' ,
' na ' =>' Namibia ' ,
' nr ' =>' Nauru ' ,
' np ' =>' Nepal ' ,
' nl ' =>' Netherlands ' ,
' an ' =>' Netherlands Antilles ' ,
' nc ' =>' New Caledonia (French) ' ,
' nz ' =>' New Zealand ' ,
' ni ' =>' Nicaragua ' ,
' ne ' =>' Niger ' ,
' ng ' =>' Nigeria ' ,
' nu ' =>' Niue ' ,
' nf ' =>' Norfolk Island ' ,
' mp ' =>' Northern Mariana Islands ' ,
' no ' =>' Norway ' ,
' om ' =>' Oman ' ,
' pk ' =>' Pakistan ' ,
' pw ' =>' Palau ' ,
' pa ' =>' Panama ' ,
' pg ' =>' Papua New Guinea ' ,
' py ' =>' Paraguay ' ,
' pe ' =>' Peru ' ,
' ph ' =>' Philippines ' ,
' pn ' =>' Pitcairn Island ' ,
' pl ' =>' Poland ' ,
' pf ' =>' Polynesia (French) ' ,
' pt ' =>' Portugal ' ,
' pr ' =>' Puerto Rico ' ,
' qa ' =>' Qatar ' ,
' re ' =>' Reunion (French) ' ,
' ro ' =>' Romania ' ,
' ru ' =>' Russia ' ,
' rw ' =>' Rwanda ' ,
' sh ' =>' Saint Helena ' ,
' kn ' =>' Saint Kitts & Nevis Anguilla ' ,
' lc ' =>' Saint Lucia ' ,
' pm ' =>' Saint Pierre and Miquelon ' ,
' vc ' =>' Saint Vincent & Grenadines ' ,
' ws ' =>' Samoa ' ,
' sm ' =>' San Marino ' ,
' st ' =>' Sao Tome and Principe ' ,
' sa ' =>' Saudi Arabia ' ,
' sn ' =>' Senegal ' ,
' rs ' =>' Serbia ' ,
' sc ' =>' Seychelles ' ,
' sl ' =>' Sierra Leone ' ,
' sg ' =>' Singapore ' ,
' sk ' =>' Slovakia ' ,
' si ' =>' Slovenia ' ,
' sb ' =>' Solomon Islands ' ,
' so ' =>' Somalia ' ,
' za ' =>' South Africa ' ,
' gs ' =>' South Georgia & South Sandwich Islands ' ,
' es ' =>' Spain ' ,
' lk ' =>' Sri Lanka ' ,
' sd ' =>' Sudan ' ,
' sr ' =>' Suriname ' ,
' sj ' =>' Svalbard and Jan Mayen Islands ' ,
' sz ' =>' Swaziland ' ,
' se ' =>' Sweden ' ,
' ch ' =>' Switzerland ' ,
' sy ' =>' Syria ' ,
' tw ' =>' Taiwan ' ,
' tj ' =>' Tajikistan ' ,
' tz ' =>' Tanzania ' ,
' th ' =>' Thailand ' ,
' tg ' =>' Togo ' ,
' tk ' =>' Tokelau ' ,
' to ' =>' Tonga ' ,
' tt ' =>' Trinidad and Tobago ' ,
' tn ' =>' Tunisia ' ,
' tr ' =>' Turkey ' ,
' tm ' =>' Turkmenistan ' ,
' tc ' =>' Turks and Caicos Islands ' ,
' tv ' =>' Tuvalu ' ,
' uk ' =>' U.K. ' ,
' ug ' =>' Uganda ' ,
' ua ' =>' Ukraine ' ,
' ae ' =>' United Arab Emirates ' ,
' uy ' =>' Uruguay ' ,
' us ' =>' USA ' ,
' um ' =>' USA Minor Outlying Islands ' ,
' uz ' =>' Uzbekistan ' ,
' vu ' =>' Vanuatu ' ,
' va ' =>' Vatican ' ,
' ve ' =>' Venezuela ' ,
' vn ' =>' Vietnam ' ,
' vg ' =>' Virgin Islands (British) ' ,
' vi ' =>' Virgin Islands (USA) ' ,
' wf ' =>' Wallis and Futuna Islands ' ,
' eh ' =>' Western Sahara ' ,
' ye ' =>' Yemen ' ,
' zm ' =>' Zambia ' ,
' zw ' =>' Zimbabwe ' );
echo "<br>Chosen Region: <select>";
foreach ($countries as $key => $value) {
echo "<option name=" . $key . ">" . $value . "</option>";
}
echo "</select><input type='submit' value='send' name='send' />
</form>";
?>
2nd Page:
<?php
$country = $_POST[$key];
echo "<img src='flags/png/' . $country . '.png'>";
?>
UPDATE: I want to relate the country to the relevant country code (which is its key in the $countries array) becuase the image names are the country codes. so when the country is clicked and sent is clicked, it takes the key (country code) puts that into the
You have to attach the name to the select tag instead of on the option in order to retrive it in the 2nd php:
echo "<br>Chosen Region: <select name='country'>";
foreach ($countries as $key => $value) {
echo "<option value=" . $key . ">" . $value . "</option>";
}
echo "</select><input type='submit' value='send' name='send' />
</form>";
2nd page
$country = $_POST['country'];
echo "<img src='flags/png/".$country.".png'>";
The problems I see are as follows:
Your
<select>
has no name, which could be a problem on the second page to identify easily what you're sending through it.On the second page I don't see where you're getting the
$key
variable to get the flag.
Suggestions:
Add a name to the so that your post variable can be used like this
$_POST['nameOfSelect']
which would contain the ID of the chosen country. Along with adding a "value" attribute to your<option>
tags.The above suggestion will fix the problem with the
$key
variable, just do the exact same thing as in your current second page$country = $_POST['nameOfSelect']
and that should solve the problem.
Hope I was clear enough and that this suggestion helps you out :)
UPDATE: Adding to the answer, specifically referring to your update, yes, adding the "value" attribute will solve your problem:
echo "<br>Chosen Region: <select name='country'>";
foreach ($countries as $key => $value) {
echo "<option value=" . $key . ">" . $value . "</option>";
}
This code will result in $_POST['country']
containing exactly the code you need, allowing you to use the image name like this:
$country = $_POST['country'];
echo "<img src='flags/png/' . $country . '.png'>";
Your code is missing the opening <select>
tag.
You have to use its name attribute as the key to access the $_POST
array to retrieve selected value.
Your html should be like this:
<select name="country">
<?php
echo "<br>Chosen Region: <select>";
foreach ($countries as $key => $value) {
echo "<option value=" . $key . ">" . $value . "</option>";
}
</select>
?>
And the php file:
<?php
$country = $_POST['country'];
?>
Your <select>
is missing its name attribute:
<select name='country'>
..options..
</select>
you're missing a name attribute for your select box. . Then in second page do $_POST["countries"]. Also, your options should have a value attribute, not name.
精彩评论