Jump to content

Trouble with dropdown selections staying selected


solonman123

Recommended Posts

Hi everyone.  My first post here.  I am a beginner and have created a contact form.  Unfortunately,  My "province_state" variable value does not retain the selection.  I would really appreciate some help in solving this.

 

Here is the link to my php file.

 

http://barrytowns.com/php/lesson_four_customer_data.php

 

And here is the code to my include file.  I have commented on line 133 that /* THIS IS WHERE MY PROBLEM IS */ to indicate the following block of echo commands.  I know I am getting something in the wrong place or order but trial and error has not given me the solution.  Thanks to anyone who can lend me a hand...

 

<?php

/*  Program name: lesson_four_customer_data.inc

  *  Description:  Defines a form that collects customer data for shipping and marketing purposes.

  */

$labels = array( "last_name" => "Last Name",

            "first_name" => "First Name",

                "street_address" => "Street Address",

            "city" => "City",

            "province_state" => "Province or State",

            "country" => "Country",           

            "postal_zip_code" => "Postal code or Zip",

            "phone_number" => "Phone number",

            "email" => "Email");

           

           

           

$province_state = array

 

        ( 

            "UNITED_STATES"=>"US",

            "Alabama" => "AL",

                "Alaska"  => "AK",

            "Arizona" => "AZ",

            "Arkansas" =>"AR",

                "California" =>"CA",

                "Colorado" =>"CO",

                "Connecticut" =>"CT",

                "Delaware" =>"DE",

                "District_of_Columbia" =>"DC",

                "Florida" => "FL",

                "Georgia" =>"GA",

                "Hawaii" =>"HI",

                "Idaho" => "ID",

                "Illinois" =>"IL",

                "Indiana"=>"IN",

                "Iowa" => "IA",

                "Kansas" =>"KS",

                "Kentucky" =>"KY",

                "Louisiana" =>"LA",

                "Maine" =>"ME",

                "Maryland" =>"MD",

                "Massachusetts" =>"MA",

                "Michigan" =>"MI",

                "Minnesota" =>"MN",

                "Mississippi" =>"MS",

                "Missouri" =>"MO",

                "Montana" =>"MT",

              "Nebraska" =>"NE",

              "Nevada" =>"NV",

              "New_Hampshire" =>"NH",

              "New_Jersey" =>"NJ",

              "New_Mexico" =>"NM",

 

              "New_York" =>"NY",

              "North_Carolina" =>"NC",

              "North_Dakota" =>"ND",

              "Ohio" =>"OH",

              "Oklahoma" =>"OK",

              "Oregon" =>"OR",

              "Pennsylvania" =>"PA",

              "Rhode_Island" =>"RI",

              "South_Carolina" =>"SC",

 

              "South_Dakota" =>"SD",

              "Tennessee" =>"TN",

              "Texas" =>"TX",

              "Utah" =>"UT",

              "Vermont" =>"VT",

              "Virginia" =>"VA",

              "Washington" =>"WA",

              "West_Virginia" =>"WV",

              "Wisconsin" =>"WI",

              "Wyoming" =>"WY",

      "CANADA"=>"Cd",

      "Alberta" =>"AB",

        "British Columbia" =>"BC",

        "Manitoba" => "MB",

          "New Brunswick" => "NB",

          "Newfoundland" =>"NF",

          "Northwest Territories" =>"NT",

          "Nova Scotia" => "NS",

            "Nunavut" =>"NU",

            "Ontario" => "ON",

            "Prince Edward Island" =>"PE",

            "Quebec" =>"QC",

            "Saskatchewan" => "SK",

            "Yukon Territory" => "YT");

           

 

 

 

 

 

$submit = "SUBMIT";

?>

<html>

<head>

 

<link href="all_over.css" rel="stylesheet" type="text/css">

<style type='text/css'>

  <!--

 

      form {

        margin: 1.5em 0 0 0;

        padding: 0;

      }

      .field {padding-bottom: 1em;}

      label {

        font-weight: bold;

        float: left;

        width: 20%;

        margin-right: 1em;

        text-align: right;

      }

      .submit {

        margin-left: 35%;

      }

    h3 {

    font-family: arial;

    text-align: center;

    }

    label {

    font-family: arial;

    font-size: 14px;

    }

  -->

</style>

</head>

<body>

<h2>Please fill out the form below to register with</h2>

<h1>SWAMPLAND PHOTOGRAPHY </h1>

<?php

/* THIS IS WHERE MY PROBLEM IS */

 

  echo "<form action='$_SERVER[php_SELF]' method='POST'>";

  foreach($labels as $field => $label)

  if($field == "province_state")

 

  {

      echo "<div class='field'><label for='$field'>$label</label>";

    echo"<select name='province_state'>";

    foreach($province_state as $value => $province_state_abr)

    {

       

        echo"<option value='$province_state_abr'>";

      if($_POST['province_state'] == $province_state_abr)

    {

          echo " selected='selected'";

    }">";

      echo"$province_state_abr";

      echo"</option>";

      }

     

      echo"</select>";

      echo"</div>";

  }

  else

  {

      echo "<div class='field'><label for='$field'>$label</label>

            <input id='$field' name='$field' type='text' value='".@$$field."'

              size='50%' maxlength='65' /></div>";

  }

 

 

 

 

   

 

   

   

   

 

 

  echo "<input type='hidden' name='submitted' value='yes'>"; 

  echo "<div id='submit'>

          <input type='submit' name='SUBMIT' value='$submit'></div>";

?>

 

</form>

 

</body>

</html>

 

 

Link to comment
Share on other sites

If you view the source you'll see where your problem is.

 

        echo"<option value='$province_state_abr'>";
       if($_POST['province_state'] == $province_state_abr)
     {
          echo " selected='selected'";
     }">";

 

You close the <option> tag, then echo an attribute. You want to get rid of that closing > and change the last part to echo ">"; and not just ">";

Link to comment
Share on other sites

I have been offered the position a couple of times, but have turned it down (I was a mod on the Hotscripts/Programmingtalk forum for a year or so and have removed enough spam posts, banned enough users, added enough code tags, ... to last me a life time.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.