Link to home
Start Free TrialLog in
Avatar of stoneycurtis
stoneycurtis

asked on

PHP Set default value in SELECT box

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.
ASKER CERTIFIED SOLUTION
Avatar of mankowitz
mankowitz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stoneycurtis
stoneycurtis

ASKER

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?
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);