PDA

View Full Version : easy question


aragon127
09-19-2002, 09:34 PM
Here's some javascrip from my site. What happens is a user clicks the dropdown box and selects a country, which populates the state list. I only use 1 country, so I'm trying to modify it so the country(whose number is 223) is automatically populated and the state box is automatically filled. I can't quite get it right. This uses a combination of javascript & php. Thanks.

<script language="javascript"><!--
function resetStateText(theForm) {
theForm.state.value = '';
if (theForm.zone_id.options.length > 1) {
theForm.state.value = "<?php echo JS_STATE_SELECT; ?>";
}
}

function resetZoneSelected(theForm) {
if (theForm.zone_id.options.length > 1) {
theForm.state.value = "<?php echo JS_STATE_SELECT; ?>";
}
}

function update_zone(theForm) {
var NumState = theForm.zone_id.options.length;
var SelectedCountry = '';

while(NumState > 0) {
NumState--;
theForm.zone_id.options[NumState] = null;
}

SelectedCountry = theForm.country.options[theForm.country.selectedIndex].value;

<?php tep_js_zone_list('SelectedCountry', 'theForm'); ?>


resetStateText(theForm);
}
//--></script>

Roy Sinclair
09-20-2002, 10:32 PM
It looks like you're making the common mistake of trying to interact between server side code (PHP) and client side code (javascript). The problem is that by the time the browser finishes rendering the page for the client and the client can interact with the page the server side code is finished. You can't call freely back and forth between the two, the mechanism for client side code interacting with server side code is the submit action of a form (or a client side simulation of that action). The way the server side code communicates with client side code it to write the communication into the client code as it writes the page.

Essentially you have two choices, refesh the page each time the country is changed or write the whole table of states into an array on the client so the client side code can perform the change. The latter is best when the table isn't very large while the former should be used for large tables so the page doesn't get oversized.