Solved

PHP Set default value in SELECT box

Posted on 2014-01-09
4
724 Views
Last Modified: 2014-05-21
Hello,
I have a list of world countries in a select drop down box, other variables are added depending on the selection but I only want the United States to be selectable.
Given that the United States 'country_id' = 223, and 'name' = "United States", how would I set the default in the following code snippet:
<select name="address[<?php echo $address_row; ?>][country_id]" onchange="country(this, '<?php echo $address_row; ?>', '<?php echo $address['zone_id']; ?>');">
                   <option value=""><?php echo $text_select; ?></option>                                                         
                    <?php foreach ($countries as $country) { ?>                                                     
                    <?php if ($country['country_id'] == $address['country_id']) { ?>                  
                    
                    <option value="<?php echo $country['country_id']; ?>" selected="selected"><?php echo $country['name']; ?></option>
                    <?php } else { ?>
                    <option value="<?php echo $country['country_id']; ?>"><?php echo $country['name']; ?></option>                    
                    <?php } ?>
                    <?php } ?>
                  </select>

Open in new window

The 'addaddress()' function looks like this:
function addAddress() {&#9;
&#9;html  = '<div id="tab-address-' + address_row + '" class="vtabs-content" style="display: none;">';
&#9;html += '  <input type="hidden" name="address[' + address_row + '][address_id]" value="" />';
&#9;html += '  <table class="form">'; 
&#9;html += '    <tr>';
    html += '&#9;   <td><span class="required">*</span> <?php echo $entry_firstname; ?></td>';
    html += '&#9;   <td><input type="text" name="address[' + address_row + '][firstname]" value="" /></td>';
    html += '    </tr>';
    html += '    <tr>';
    html += '      <td><span class="required">*</span> <?php echo $entry_lastname; ?></td>';
    html += '      <td><input type="text" name="address[' + address_row + '][lastname]" value="" /></td>';
    html += '    </tr>';
    html += '    <tr>';
    html += '      <td><?php echo $entry_company; ?></td>';
    html += '      <td><input type="text" name="address[' + address_row + '][company]" value="" /></td>';
    html += '    </tr>';&#9;
    html += '    <tr class="company-id-display">';
    html += '      <td><?php echo $entry_company_id; ?></td>';
    html += '      <td><input type="text" name="address[' + address_row + '][company_id]" value="" /></td>';
    html += '    </tr>';
    html += '    <tr class="tax-id-display">';
    html += '      <td><?php echo $entry_tax_id; ?></td>';
    html += '      <td><input type="text" name="address[' + address_row + '][tax_id]" value="" /></td>';
    html += '    </tr>';&#9;&#9;&#9;
    html += '    <tr>';
    html += '      <td><span class="required">*</span> <?php echo $entry_address_1; ?></td>';
    html += '      <td><input type="text" name="address[' + address_row + '][address_1]" value="" /></td>';
    html += '    </tr>';
    html += '    <tr>';
    html += '      <td><?php echo $entry_address_2; ?></td>';
    html += '      <td><input type="text" name="address[' + address_row + '][address_2]" value="" /></td>';
    html += '    </tr>';
    html += '    <tr>';
    html += '      <td><span class="required">*</span> <?php echo $entry_city; ?></td>';
    html += '      <td><input type="text" name="address[' + address_row + '][city]" value="" /></td>';
    html += '    </tr>';
    html += '    <tr>';
    html += '      <td><span id="postcode-required' + address_row + '" class="required">*</span> <?php echo $entry_postcode; ?></td>';
    html += '      <td><input type="text" name="address[' + address_row + '][postcode]" value="" /></td>';
    html += '    </tr>';
&#9;html += '    <tr>';
    html += '      <td><span class="required">*</span> <?php echo $entry_country; ?></td>';
    html += '      <td><select name="address[' + address_row + '][country_id]" onchange="country(this, \'' + address_row + '\', \'0\');">';
    html += '         <option value="223"><?php echo $text_select; ?></option>';
    <?php foreach ($countries as $country) { ?>
    html += '         <option value="<?php echo $country['country_id']; ?>"><?php echo addslashes($country['name']); ?></option>';
    <?php }?>
    html += '      </select></td>';
    html += '    </tr>';
    html += '    <tr>';
    html += '      <td><span class="required">*</span> <?php echo $entry_zone; ?></td>';
    html += '      <td><select name="address[' + address_row + '][zone_id]"><option value="false"><?php echo $this->language->get('text_none'); ?></option></select></td>';
    html += '    </tr>';
&#9;html += '    <tr>';
    html += '      <td><?php echo $entry_default; ?></td>';
    html += '      <td><input type="radio" name="address[' + address_row + '][default]" value="1" /></td>';

Open in new window

Thanks in advance for any help.
0
Comment
Question by:stoneycurtis
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
Comment Utility
if you include "disabled"  within the option element, you can have a disabled option

You probably want something like this:

<select name="address[<?php echo $address_row; ?>][country_id]" onchange="country(this, '<?php echo $address_row; ?>', '<?php echo $address['zone_id']; ?>');">
                   <option value=""><?php echo $text_select; ?></option>                                                         
                    <?php foreach ($countries as $country) { ?>                                                     
                    <?php if ($country['country_id'] == 223) { ?>                  
                    <option value="<?php echo $country['country_id']; ?>" disabled><?php echo $country['name']; ?></option>
                    <?php } else if ($country['country_id'] == $address['country_id']) { ?>                                      
                    <option value="<?php echo $country['country_id']; ?>" selected="selected"><?php echo $country['name']; ?></option>
                    <?php } else { ?>
                    <option value="<?php echo $country['country_id']; ?>"><?php echo $country['name']; ?></option>                    
                    <?php } ?>
                    <?php } ?>
                  </select>

Open in new window

0
 

Author Comment

by:stoneycurtis
Comment Utility
Thanks Mankowitz for your suggestion, unfortunately it didn't work, it didn't break the form, everything works as it did prior to the change. Do you think the function (Line 42 above) is over-riding?
0
 
LVL 24

Expert Comment

by:mankowitz
Comment Utility
possibly. lets make sure that you are supplying the right data to your function. use var_dump() to see what's in those variables.

var_dump ($address_row);
var_dump ($address);
var_dump ($text_select);
var_dump ($countries);
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now