开发者

PHP how to get a dropdown list with countries

i'm making a country list for my registration page, but i'm wondering how to implement it into my registration form.

i made the list in the file country.php

<select name="Country">
<option selected="selected" </option>
<option value="">Country...&l开发者_开发百科t;/option>
<option value="Afganistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option> 
</select>

and in my registration page i use in my table

  <tr>
  <th>Land </th>
  <td><?php include 'country.php'; ?></td>
  </tr>

But it doesnt seem to keep the value's in the form when i submit it.

how do i make the value of $Country equal to the option selected in the country.php file?

Thanks a lot :)


<option selected="selected" </option>

should be

<option selected="selected"></option> 


I think line break your code:

<option selected="selected" </option>

So all you need is to enclose <option> tag.

You can also extract country list from any site have it like Yahoo, just go to singup page, then from the browser go to view=>page source.


Aside of the syntax errors other people have pointed, you'd need to save all the countries inside an array, and loop the array echoing one option/country per iteration. And, the part that makes the select remember what you entered before is putting the selected="selected" piece inside the chosen country option.

So overall it may look like this:

function getSelectOfCountries($chosenCountry = null)
{
  $countries = array('Afganistan', 'Albania', 'Algeria', ...);
  echo "<select name='country' id='country'>\n";
  echo "<option value=''>Country...</option>\n";
  foreach ($countries as $country)
  {
    echo "<option value='$country'";
    if ($chosenCountry == $country)
    {
      echo " selected='selected'";
    }
    echo ">$country</option>\n";
  }
}

You put that function in a included file. And in the form:

<?php
if (isset($_GET['country']))
{
  $country = $_GET['country'];
} else {
  $country = null;
}
?>
<form ...>
...
<tr>
  <th><label for="country">Land </label></th>
  <td><?php getSelectOfCountries($country);?></td>
</tr>
...
</form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜