Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

AJAX make country change link state change


dragon2934

Recommended Posts

javascript file as selectzone.js

var xmlHttp

 

 

function showZone(str)

{

xmlHttp=GetXmlHttpObject()

if (xmlHttp==null)

{

alert ("Browser does not support HTTP Request")

return

}

if(str=="0")

str=document.checkout_payment.country.value;

var url="getzone.php"

url=url+"?q="+str

url=url+"&sid="+Math.random()

xmlHttp.onreadystatechange=stateChanged

xmlHttp.open("GET",url,true)

xmlHttp.send(null)

}

 

function stateChanged(divname)

{

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

{

document.getElementById('zoneid').innerHTML=xmlHttp.responseText

}

}

 

function GetXmlHttpObject()

{

var objXMLHttp=null

if (window.XMLHttpRequest)

{

objXMLHttp=new XMLHttpRequest()

}

else if (window.ActiveXObject)

{

objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")

}

return objXMLHttp

}

 

getzone.php

<?php

 

require('includes/application_top.php');

 

$country=$HTTP_GET_VARS['q'];

$zones_array = array();

$zones_query = tep_db_query("select zone_name,zone_id from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");

$default_zone="";

$ZoneCount=0;

while ($zones_values = tep_db_fetch_array($zones_query)) {

$ZoneCount++;

$zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);

if(STORE_ZONE==$zones_values['zone_id'])

$default_zone=$zones_values['zone_name'];

}

if($ZoneCount>0){

if(strlen($default_zone)>0){

echo tep_draw_pull_down_menu('state', $zones_array,$default_zone);

}else{

echo tep_draw_pull_down_menu('state', $zones_array);

}

}else{

echo tep_draw_input_field('state');

}

if (tep_not_null(ENTRY_STATE_TEXT)) echo ' <span class="inputRequirement">' . ENTRY_STATE_TEXT;

?>

 

 

 

when using it,first should include it

<script language="javascript" src="selectzone.js"></script>

 

then make some change so that it look like this

<div id="zoneid" class="main" >

<?php

// if ($process == true) {

/*

if ($entry_state_has_zones == true) {

$country=STORE_COUNTRY;

$zones_array = array();

$zones_query = tep_db_query("select zone_name,zone_id from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");

$default_zone="";

while ($zones_values = tep_db_fetch_array($zones_query)) {

$zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);

if(STORE_ZONE==$zones_values['zone_id'])

$default_zone=$zones_values['zone_name'];

}

if(strlen($default_zone)>0){

echo tep_draw_pull_down_menu('state', $zones_array,$default_zone);

}else{

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></div>

<?php

echo '<script language="JavaScript">showZone("'.STORE_COUNTRY.'","zoneid");</script>';

 

?>

 

</td>

</tr>

<?php

}

?>

<tr>

<td class="main"><?php echo ENTRY_COUNTRY; ?></td>

<td class="main"><?php echo tep_get_country_list('country', (int)STORE_COUNTRY,' onchange="showZone(\'0\',\'zoneid\');"') . ' ' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>

</tr>

</table></td>

</tr>

</table></td>

 

 

ok,done,when you select a country,the state will be changed

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...