0

Hi I am trying to store values chosen in a forms array. I use the form values as index to find the values in an array that has the actual word. ex "nf" => Newfoundland

<!-- PROVINCES CHOICE -->
Province (Multiple Select) <font style="color:red;">*</font>
     <select name="province[]" multiple = "multiple" style = "background-color:<?php echo $colorProvince; ?>">                                      
        <!-- Option select for province -->
        <option value="--" disabled="disabled">--Please Select Provinces--</option>
        <option value="nf">Newfoundland</option>
        <option value="pe">Prince Edward Island</option>
        <option value="nb">New Brunswick</option>
        <option value="ns">Nova Scotia</option>
        <option value="qc">Quebec</option>
        <option value="on">Ontario</option>
        <option value="mb">Manitoba</option>
        <option value="sk">Saskatchewan</option>
        <option value="ab">Alberta</option>
        <option value="bc">British Columbia</option>
        <option value="nt">Northwest Territories</option>
    </select>

    <?PHP $PROVINCES = array(
        "--"=>"---Please Select Provinces---",                
        "nf"=>"Newfoundland",
        "pe"=>"PrinceEdwardIsland",
        "nb"=>"New Brunswick",
        "ns"=>"Nova Scotia",
        "qc"=>"Quebec",
        "on"=>"Ontario",
        "mb"=>"Manitoba",
        "sk"=>"Saskatchewan",
        "ab"=>"Alberta",
        "bc"=>"British Columbia",
        "nt"=>"Northwest Territories");

    {
        foreach($PROVINCES as $prov => $selectedProvince)               
        {
            $province = $province . $selectedProvince . " , ";              

        }
        echo $province;
   ?>

I think I am just filling the variable wrong using the province array and the forms array. Any help and explanation you can provide is excellent. Thank you.

6
  • you have syntax error in echo here echo: $province; ? Commented Oct 6, 2014 at 9:07
  • $province .= "$prov - $selectedProvince, "; This will concatenate keys and values of the array Commented Oct 6, 2014 at 9:08
  • I dont need the keys, only the values. So if someone clicked in the form Manitoba and Alberta, it should look in the PROVINCE array for "mb" and "ab" and display there values of Manitoba and Alberta Commented Oct 6, 2014 at 9:13
  • @null Sorry no that was a typo, I added it for ease of mine for others, the actual echo out is longer but I tested it and thats not the problem Commented Oct 6, 2014 at 9:14
  • What you are actually trying to do here? Are you processing the results of the form? Can you post the output you're getting and what you're expecting to get? Your question is rather unclear. Commented Oct 6, 2014 at 9:26

1 Answer 1

0

At the moment, you are not searching for the values that were selected in your form in the keys in $PROVINCES, so you're not getting the results you want.

When you are processing the form data, you need to correlate the values from $_POST['province'] (or $_GET['province'] if it's a GET form), and then translate them using your array of abbreviations and full titles. However, since you have this list of abbreviations and titles, you can also use them to populate the select element when you create the form. Here is some code to demonstrate that.

Early in the script, declare the array of provinces and abbreviations:

$p_arr = array(
    "ab"=>"Alberta",
    "bc"=>"British Columbia",
    "mb"=>"Manitoba",
    "nb"=>"New Brunswick",
    "nf"=>"Newfoundland",
    "nt"=>"Northwest Territories",
    "ns"=>"Nova Scotia",
    "on"=>"Ontario",
    "pe"=>"Prince Edward Island",
    "qc"=>"Quebec",
    "sk"=>"Saskatchewan",
);
?>

Later on, in the form creation code:

<label for="province_select">Province (multiple select) <em class="rqd">*</em></label>
<select id="province_select" name="province[]" multiple="multiple">
    <option value="--" disabled="disabled">--Please Select Provinces--</option>
<?php
    # add the options using the keys and values from the abbreviation/province
    foreach ($p_arr as $pk => $pv) {
        echo
'       <option value="' . $pk . '">' . $pv . "</option>\n";
    }
?>

And in the code where you're processing input from the form:

<?php
    # this function gets the province name from $p_arr when given an abbreviation
    function get_province( $p ) {
        if (isset($GLOBALS['p_arr'][$p])) {
            return $GLOBALS['p_arr'][$p];
        }
    }

    if ($_POST) {  // or $_GET
        # if 'province' is set in the form
        if ($_POST['province']) {
            # create $prov_str by applying the function "get_province" to all the
            # values in $_POST['province'], and join them with ", "
            $prov_str = implode(", ", array_map( "get_province", $_POST['province'] ) );
            echo "$prov_str\n";
        }
    }

Sample input: bc, nf, ns, qc

Output: British Columbia, Newfoundland, Nova Scotia, Quebec

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

Comments

Your Answer

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