Solved

PHP Set default value in SELECT box

Posted on 2014-01-09
4
769 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Part One of the two-part Q&A series with MalwareTech.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

615 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