0

I am trying to create a form using php. I am unsure how to put the country drop down menu right after the city field. I am not sure the best way to do this I have my array.

$labels = array (
    "first_name" => "First Name",
    "last_name" => "Last Name",
    "address" => "Address",
    "city" => "City",
    "email" => "E-mail",
    "phone" => "Phone", );

$country = array (
    "select" => "",
    "us" => "United States",
    "ca" => "Canada",
    "mx" => "Mexico", );    

$submit = "Submit";
?>

Here is the display code:

<?php
    echo "<h2>Customer Info</h2>";
echo "<form action='checkBlank.php' method='post'>";
    foreach ( $labels as $field => $label)
    {
        echo "<div class='field'>
                <label for='$field'>$label</label>
                    <input id='$field' name='$field' type='text'
                        size='42' /></div>";
        if($field == "city") {
            echo "<label for='country'>Country</label>
                    <select id='country' name='country'>";

                foreach ( $country as $select => $option)
                {
                    echo "<option value='$value'>$option</option>";
                }

            echo "</select>";
        }
    }
        echo "<div id='submit'>
            <input type='submit' value='$submit'></div>
            </form>";
?>
2
  • 1
    dropdowns use <select> and <option>, why the input? Commented Apr 13, 2015 at 21:19
  • input is for the field and submit button tags of the form, I haven't put the drop down tag in there yet as I am not sure where it should be put so that php will still display the $labels array while putting the select before the e-mail and phone option and after the city field. Commented Apr 13, 2015 at 21:21

3 Answers 3

1

Test the field name, and if it's the city, do another loop to display the country drop-down.

foreach ( $labels as $field => $label)
{
    echo "<div class='field'>
            <label for='$field'>$label</label>
                <input id='$field' name='$field' type='text'
                    size='42' /></div>";
    if ($field == 'city') {
        echo '<div><select name="country">';
        foreach ($country as $short => $long) {
            echo "<option value='$short'>$long</option>";
        }
        echo '</select></div>';
    }
}

BTW, it's conventional to use an empty value for the Select One option. Standard form validation tools will recognize this as meaning no option was selected, if you mark the field as required.

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

1 Comment

thanks barmar I have changed the select to blank and will mark the field as required
0

'Looks like @Barmar beat me to the punch on this one but i have a similar solution

$labels = array (
"first_name" => "First Name",
"last_name" => "Last Name",
"address" => "Address",
"city" => "City",
"email" => "E-mail",
"phone" => "Phone", );

$country = array (
"select" => "Select One",
"us" => "United States",
"ca" => "Canada",
"mx" => "Mexico", );    

$submit = "Submit";


echo "<h2>Customer Info</h2>";
echo "<form action='checkBlank.php' method='post'>";
    foreach ( $labels as $field => $label)
    {
        echo "<div class='field'>
                <label for='$field'>$label</label>
                    <input id='$field' name='$field' type='text'
                        size='42' /></div>";
        if($field == "city") {
            echo "<label for='country'>Country</label><select id='country' name='country'>";

                foreach ( $country as $value => $option)
                {
                    echo "<option value='$value'>$option</option>";
                }

            echo "</select>";
        }
    }

    echo "<div id='submit'>
        <input type='submit' value='$submit'></div>
        </form>";

9 Comments

So I put it as i see it above, it still only lists it as a field, but will only show the mexico option and not the other 2?
That's odd, I have it working fine on my page here, do you have any other code on that page that may be interfering?
would internal css for the form effect it? other than that its exactly as you have it above
I wouldn't think so, try placing the php above in a blank file and uploading it to your web server and see if you have the same results
hmmmm, ok well it does display as a drop down when previewed locally as a .php and on the server. When I save it as form.inc and use the include function to display it does the displaying as a field with only mexico as an option.
|
0

I would personally do it like this:

 $fields = array (
    array(
        "name" => "first_name",
        "label" => "First Name",
    ),
    array(
        "name" => "last_name",
        "label" => "Last Name",
    ),
    array(
        "name" => "address",
        "label" => "Address",
    ),
    array(
        "name" => "city",
        "label" => "City",
    ),
    array(
        "name" => "country",
        "label" => "Country",
        "options" => array (
            "" => "Select One",
            "us" => "United States",
            "ca" => "Canada",
            "mx" => "Mexico", 
        ),  
    ),
    array(
        "name" => "email",
        "label" => "E-mail",
    ),
    array(
        "name" => "phone",
        "label" => "Phone",
    ),
);

foreach($fields as $field) {
    echo "<div class='field'><label for='{$field['name']}'>{$field['label']}</label>";
    if (isset($field['options'])) {
        echo "<select name='{$field['name']}'>";
        foreach ($option as $value => $title) {
            echo "<option value='$value'>$title</option>";
        }
        echo '</select>'
    } else {
       echo "<input id='{$field['name']}' name='{$field['name']}' type='text' size='42' />";
    }
    echo "</div>";
}

Comments

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.