Reputation: 2721
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'].
Upvotes: 1
Views: 461
Reputation: 173572
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 />';
?>
Upvotes: 2
Reputation: 5975
<?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" />
Upvotes: 1
Reputation: 2170
<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
Upvotes: 1