Solved

PHP Set default value in SELECT box

Posted on 2014-01-09
4
742 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
ID: 39769867
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
ID: 39772100
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
ID: 39774650
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

821 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