Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Setting default country in countries drop down


Guest

Recommended Posts

I will really appreciate if you can give me step by step instructions of how to delete the drop down box for the country and hard code text. If possible let me know how to delete the text boxes for city and state also and hard code it to display a specific country/city/state.
The code that displays the text boxes and drop down is
			  <tr>
			<td class="main"><?php echo ENTRY_CITY; ?></td>
			<td class="main"><?php echo tep_draw_input_field('city') . ' ' . (tep_not_null(ENTRY_CITY_TEXT) ? '<span class="inputRequirement">' . ENTRY_CITY_TEXT . '</span>': ''); ?></td>
		  </tr>
<?php
 if (ACCOUNT_STATE == 'true') {
?>
		  <tr>
			<td class="main"><?php echo ENTRY_STATE; ?></td>
			<td class="main">
<?php
if ($process == true) {
  if ($entry_state_has_zones == true) {
	$zones_array = array();
	$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");
	while ($zones_values = tep_db_fetch_array($zones_query)) {
	  $zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
	}
	echo tep_draw_pull_down_menu('state', $zones_array);
  } else {
	echo tep_draw_input_field('state');
  }
} else {
  echo tep_draw_input_field('state');
}

if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT;
?>
			</td>
		  </tr>
<?php
 }
?>
		  <tr>
			<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
			<td class="main"><?php echo tep_get_country_list('country') . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
		  </tr>

If all that you want is to replace this with hard coded inputs, it would look like

			  <tr>
			<td class="main"><?php echo ENTRY_CITY; ?></td>
			<td class="main"><?php echo tep_draw_hidden_field('city', 'Toronto') . 'Toronto' . ' ' . (tep_not_null(ENTRY_CITY_TEXT) ? '<span class="inputRequirement">' . ENTRY_CITY_TEXT . '</span>': ''); ?></td>
		  </tr>
<?php
 if (ACCOUNT_STATE == 'true') {
?>
		  <tr>
			<td class="main"><?php echo ENTRY_STATE; ?></td>
			<td class="main">
<?php
echo tep_draw_hidden_field('state', 'Ontario') . 'Ontario';

if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT;
?>
			</td>
		  </tr>
<?php
 }
?>
		  <tr>
			<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
			<td class="main"><?php echo tep_draw_hidden_field('country', '38') . 'Canada' . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
		  </tr>

The '38' is the code for Canada. I got that by looking at the HTML for the select box:

<option value="38">Canada</option>

The United States is 223 and the UK is 222. You could also get these values by looking in the countries table in the database.

Always back up before making changes.

Link to comment
Share on other sites

Problem is that when you select another country it doesn't remove the states drop down for the default country you selected. I believe this may be fixable with javascript, but I wouldn't know where to start.

Link to comment
Share on other sites

Problem is that when you select another country it doesn't remove the states drop down for the default country you selected. I believe this may be fixable with javascript, but I wouldn't know where to start.
In a stock osCommerce, in includes/form_check.js.php, just before the
//--></script>

add

<?php 
 $countries_with_zones = array();
 $countries_with_zones_query = tep_db_query("SELECT c.countries_iso_code_2 FROM countries c, zones z WHERE c.countries_id = z.zone_country_id GROUP BY z.zone_country_id ORDER BY z.zone_country_id");
 while ( $country_with_zone = tep_db_fetch_array($countries_with_zones_query) ) {
$countries_with_zones[] = $country_with_zone['countries_iso_code_2'];
 }
?>
function displayStateInputs(value) {
 var state_input = document.getElementById('state');
<?php
 foreach ($countries_with_zones as $country_with_zone_code) {
?>
 var <?php echo $country_with_zone_code; ?>_states_menu = document.getElementById('<?php echo $country_with_zone_code; ?>_states');
  <?php echo $country_with_zone_code; ?>_states_menu.style.display = "none";
<?php
 }
?>

 switch (value) {
<?php
 foreach ($countries_with_zones as $country_with_zone_code) {
?>
case '<?php echo $country_with_zone_code; ?>':
  <?php echo $country_with_zone_code; ?>_states_menu.style.display = "";
  state_input.value = <?php echo $country_with_zone_code; ?>_states_menu.value;
  state_input.style.display = "none";
  break;
<?php
 }
?>
default:
  state_input.value = '';
  state_input.style.display = "";
 }
}
function updateState(value) {
 document.getElementById('state').value = value;
}

In create_account.php (around line 18), before

  $process = false;
 if (isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'process')) {
$process = true;

add

  $state = '';

Note that you could set this to a particular state if you want your drop down to default to a particular state.

 

In create_account.php, around line 263, change

<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">

to

<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" onload="displayStateInputs('US')">

Replace US with another country's two character ISO code as appropriate or set to an empty string (two single quotes with nothing between them) to default to showing a text box rather than a select menu. This value should correspond with the value that you set for your countries drop down menu default (leave empty if you are not setting a default for your countries drop down).

 

Around lines 411 to 439, change

<?php
 if (ACCOUNT_STATE == 'true') {
?>
		  <tr>
			<td class="main"><?php echo ENTRY_STATE; ?></td>
			<td class="main">
<?php
if ($process == true) {
  if ($entry_state_has_zones == true) {
	$zones_array = array();
	$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");
	while ($zones_values = tep_db_fetch_array($zones_query)) {
	  $zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
	}
	echo tep_draw_pull_down_menu('state', $zones_array);
  } else {
	echo tep_draw_input_field('state');
  }
} else {
  echo tep_draw_input_field('state');
}

if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
			</td>
		  </tr>
<?php
 }
?>

to

<?php
 if (ACCOUNT_STATE == 'true') {
?>
		  <tr>
			<td class="main"><?php echo ENTRY_STATE; ?></td>
			<td class="main">
<?php
$zones_array = array();
$select_zones_query = tep_db_query("SELECT c.countries_iso_code_2, z.zone_name FROM countries c, zones z WHERE c.countries_id = z.zone_country_id ORDER BY z.zone_country_id, z.zone_name");
while ($zone_option = tep_db_fetch_array($select_zones_query)) {
  $country_with_zone_code = $zone_option['countries_iso_code_2'];

  if (!is_array($zones_array[$country_with_zone_code]) {
	$zones_array[$country_with_zone_code] = array(array('id' => '', 'text' => PLEASE_SELECT_A_STATE));
  }

  $zones_array[$country_with_zone_code][] = array('id' => $zone_option['zone_name'], 'text' => $zone_option['zone_name']);
}
foreach ( $zones_array as $country_with_zone_code => $country_zones_array) {
  echo tep_draw_pull_down_menu($country_with_zone_code . '_state', $country_zones_array, $state, 'onchange="displayStateInputs(this.value);"');
}
echo tep_draw_input_field('state', '', 'onchange="updateState(this.value)"');

if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
			</td>
		  </tr>
<?php
 }
?>

and in includes/languages/english/create_account.php (or whatever language), add

  define('PLEASE_SELECT_A_STATE', 'Please select a state');

Note: I haven't tested this. Backup before trying.

Always back up before making changes.

Link to comment
Share on other sites

Parse error: syntax error, unexpected '{' in /homepages/8/d274908695/htdocs/create_account.php on line 435

 

Line 435 - if (!is_array($zones_array[$country_with_zone_code]) {

 

This is the error I get.

Link to comment
Share on other sites

Revised version (also fixes missing parenthesis):

 

In a stock osCommerce, in includes/form_check.js.php, just before the

//--></script>

add

<?php
 $countries_with_zones = array();
 $country_id_for = array();
 $countries_with_zones_query = tep_db_query("SELECT c.countries_id, c.countries_iso_code_2 FROM countries c, zones z WHERE c.countries_id = z.zone_country_id GROUP BY z.zone_country_id ORDER BY z.zone_country_id");
 while ( $country_with_zone = tep_db_fetch_array($countries_with_zones_query) ) {
$countries_with_zones[] = $country_with_zone['countries_iso_code_2'];
$country_id_for[$country_with_zone['countries_iso_code_2']] = $country_with_zone['countries_id'];
 }
?>
function displayStateInputs(value) {
 var state_input = document.getElementsByName('state')[0];
<?php
 foreach ($countries_with_zones as $country_with_zone_code) {
?>
 var <?php echo $country_with_zone_code; ?>_states_menu = document.getElementsByName('<?php echo $country_with_zone_code; ?>_state')[0];
 <?php echo $country_with_zone_code; ?>_states_menu.style.display = 'none';
<?php
 }
?>

 switch (value) {
<?php
 foreach ($countries_with_zones as $country_with_zone_code) {
?>
case '<?php echo $country_id_for[$country_with_zone_code]; ?>':
  <?php echo $country_with_zone_code; ?>_states_menu.style.display = '';
  state_input.value = <?php echo $country_with_zone_code; ?>_states_menu.value;
  state_input.style.display = 'none';
  break;
<?php
 }
?>
default:
  state_input.value = '';
  state_input.style.display = '';
 }
}
function updateState(value) {
 document.getElementsByName('state')[0].value = value;
}

In create_account.php (around line 18), before

  $process = false;
 if (isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'process')) {
$process = true;

add

  $state = '';
 $country = '223';

Note that you could set $state to a particular state (e.g. $state = 'Alabama';) if you want your drop down to default to a particular state. Same thing for country. Setting it to 223 makes the US the default. Set it appropriately for your store. Setting it to an empty string would make it show the countries drop down with no default and the text input for state (what create_account.php does now).

 

In create_account.php, around line 263, change

<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">

to

<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" onload="displayStateInputs('<?php echo $country; ?>')">

Around lines 411 to 443, change

<?php
 if (ACCOUNT_STATE == 'true') {
?>
		  <tr>
			<td class="main"><?php echo ENTRY_STATE; ?></td>
			<td class="main">
<?php
if ($process == true) {
  if ($entry_state_has_zones == true) {
	$zones_array = array();
	$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");
	while ($zones_values = tep_db_fetch_array($zones_query)) {
	  $zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
	}
	echo tep_draw_pull_down_menu('state', $zones_array);
  } else {
	echo tep_draw_input_field('state');
  }
} else {
  echo tep_draw_input_field('state');
}

if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
			</td>
		  </tr>
<?php
 }
?>
		  <tr>
			<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
			<td class="main"><?php echo tep_get_country_list('country') . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
		  </tr>

to

<?php
 if (ACCOUNT_STATE == 'true') {
?>
		  <tr>
			<td class="main"><?php echo ENTRY_STATE; ?></td>
			<td class="main">
<?php
$zones_array = array();
$select_zones_query = tep_db_query("SELECT c.countries_iso_code_2, z.zone_name FROM countries c, zones z WHERE c.countries_id = z.zone_country_id ORDER BY z.zone_country_id, z.zone_name");
while ($zone_option = tep_db_fetch_array($select_zones_query)) {
  $country_with_zone_code = $zone_option['countries_iso_code_2'];

  if (!is_array($zones_array[$country_with_zone_code])) {
	$zones_array[$country_with_zone_code] = array(array('id' => '', 'text' => PLEASE_SELECT_A_STATE));
  }

  $zones_array[$country_with_zone_code][] = array('id' => $zone_option['zone_name'], 'text' => $zone_option['zone_name']);
}
foreach ( $zones_array as $country_with_zone_code => $country_zones_array) {
  echo tep_draw_pull_down_menu($country_with_zone_code . '_state', $country_zones_array, $state, 'onchange="updateState(this.value)" style="display:none"');
}
echo tep_draw_input_field('state');

if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
			</td>
		  </tr>
<?php
 }
?>
		  <tr>
			<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
			<td class="main"><?php echo tep_get_country_list('country', $country, 'onchange="displayStateInputs(this.value)"') . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
		  </tr>

and in includes/languages/english/create_account.php (or whatever language), add

  define('PLEASE_SELECT_A_STATE', 'Please select a state');

You also need to have the charset set appropriately for the various states you will show. UTF-8 might work, although the osCommerce javascript may then fail in a browser using Shift-JIS (Japanese encoding).

 

Backup before trying.

Always back up before making changes.

Link to comment
Share on other sites

  • 1 year later...

This is something I had wrestled with for some time, until the obvious hit me..

 

I tried various ways to output a sorted countries list, changing the country I wanted at top to be ID 0 and whatnot, didnt work. Finally I relaized I was overlooking the obvious of using <option .... selected>

 

Thankfully, the requisite functions have built-in "selected" handling, thus setting your default country is easy.

 

Edit create_account.php and find

 

              <tr>
               <td class="main"><?php echo ENTRY_COUNTRY; ?></td>
               <td class="main"><?php echo tep_get_country_list('country') . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
             </tr>

 

you want to change this line

php echo tep_get_country_list('country') .

to be

php echo tep_get_country_list('country','223') .

223 == the country ID for United States. YOu can set that to be any country by looking either in the database countries list for your countries ID, or even easier, look at the HTML of the create_account.php page and find where the country drop down list is, search for your country and grab the "value".

 

Hope this helps.

 

Wow! Thank you. Exactly what I needed and provided a perfect fix. Now I can move on to the next project lol

Link to comment
Share on other sites

  • 10 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...