1

This is a basic question I'm sure....

I have a normal form, but one of the fields is a Country dropdown menu that I am populating with an external xml script:

<?php 
//Build countries dropdown from GeoNames database
$xmlcountries = 'http://ws.geonames.org/countryInfo';
echo '<select name="custom_country" id="custom_country">';
$countries = simplexml_load_file($xmlcountries);
echo '<option value="">Your country</option>';
foreach ($countries->country as $country) {
    echo '<option value="'.$country->geonameId.'|'.$country->countryName.'">'.$country->countryName.'</option>';
}
echo '</select>';

?>

My form is this:

<form action="update.php" method="post">
    Country:<br />
    <input type="text" name="Country" size="100" /><br />
    State:<br />
    <input type="text" name="State" size="100" /><br />
    City:<br />
    <input type="text" name="City" size="100" /><br />
    <input type="submit" value="Update Database" />
</form>

I'd like the above $country variable to take the place of the name="Country" field in the form. How do I do that?

To be clear-when I submit the form, I want the value from the country dropdown to populate the $_POST['Country'].

3
  • You mean that the value of <input name="Country" /> must be set to the the value chosen from the select list? Commented Jul 5, 2012 at 6:34
  • please clear the problem, you want to place country name to <input type="text" name="Country" size="100" /> which is selected from dropdown or anything else? Commented Jul 5, 2012 at 6:34
  • I want to get rid of the ability of the user to type in the Country and have to pick it from the dropdown and have that be the $_POST['Country'] variable. Commented Jul 5, 2012 at 6:36

3 Answers 3

2

It seems like you just want to replace:

<input type="text" name="Country" size="100" /><br />

With this:

<?php

echo '<select name="Country" id="custom_country">';
$countries = simplexml_load_file($xmlcountries);
echo '<option value="">Your country</option>';
foreach ($countries->country as $country) {
    echo '<option value="'.$country->geonameId.'|'.$country->countryName.'">'.$country->countryName.'</option>';
}
echo '</select><br />';

?>
Sign up to request clarification or add additional context in comments.

Comments

1
<input type="text" name="Country" value="<?php echo $country; ?>" size="100" /><br />

The value parameter will prefill that field with whatever you specify, in this case country.

Beware unless you tell it otherwise, this can be changed by the user, so that may be an issue for you depending on how you are using.

The value attribute is used differently for different input types:

For "button", "reset", and "submit" - it defines the text on the button For "text", "password", and "hidden" - it defines the initial (default) value of the input field For "checkbox", "radio", "image" - it defines the value associated with the input (this is also the value that is sent on submit) More Info

Comments

1
<?php 
...
echo '<select name="custom_country" id="custom_country" onchange="document.getElementById(\'Country\').value = this.value">';
?>

<input id="Country" type="text" name="Country" size="100" readonly="readonly" />

3 Comments

You could also use JQuery to hook the onchange event, of course - but I considered that to be slightly out of scope for this question
hmm....my form isn't submitting now. are you putting the echo... just below the echo '</select>'; line?
no, instead of the "echo '<select ..." that you already have on line 4 of your original post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.