Forum Moderators: coopster

Message Too Old, No Replies

onChange working in Firefox, but not IE

I cannot get an onchange command to fire in IE though it works in firefox

         

TWelch

8:04 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



Hello :)

I am working a form that will have a state select menu and a county menu. When the user selects a state, the county menu should then be populated. This code works in Firefox beautifully, but I cannot get it to work in IE:

Java portion:

<SCRIPT TYPE="text/javascript">
<!--
function resubmit()
{
document.addanad.action="addad.php";
document.addanad.submit();
}
function process()
{
document.addanad.action="processad.php";
document.addanad.submit();
}
//-->
</script>

HTML/PHP portion:

<td>State:</td>
<td><select name="state" onchange="javascript:resubmit()">
<?php
$getstatesquery="SELECT DISTINCT loc_state FROM ads_location ORDER BY loc_state";
$getstates=mysql_query($getstatesquery) or die ("Could not acquire States list");
$statesnum=mysql_num_rows($getstates);
$count=0;
if ($value=="")
{
echo "<option selected value =\"\">Select a State</option>";
}
else
{
echo "<option selected value =\"$state\">$state</option>";
}
while ($states=mysql_fetch_row($getstates))
{
if ($value=="")
{
echo "<option>$states[0]</option>";
}
if (!$value=="")
{

echo "<option>$states[0]</option>";
$selected=$state;
}

}
$count++;
?>
</select></td>

Any ideas why this doesn't work in IE?

Birdman

8:26 pm on Jan 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try removing the "javascript:" portion:

onchange="resubmit()">

The javascript: bit is normally used in the href="" attribute.

BTW, welcome to WW!

TWelch

8:40 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



Thanks for the welcome!

Actually, my original code did not include the "javascript:" in "javascript:resubmit()" - that was one of the things I tried changing to get it to work ;)

It didn't work then either :/

TWelch

4:02 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



No additional suggestions?

I have quite literally pulled my hair out over this :/

Is there another way to do this using PHP without totally reloading the page on a select change?

Birdman

4:55 pm on Jan 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you say it doesn't work in IE, what exactly happens?

Also, I don't see any county menu code. I'm trying to figure the problem but I think we'll need to see the whole script(at least anything related to the form).

Is there another way to do this using PHP without totally reloading the page on a select change?

Actually, yes! But if there are too many submenu variations, it will cause javascript overhead.

Let's see the rest of code and we'll get it straight.

TWelch

5:18 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



The full code:

<?php
include('constants.php');
include('functions.inc');
$connect=connection($dbname);

$value = "";
if ( isset( $_POST[ 'state' ] ) ) {
$value = $_POST[ 'state' ];
}

?>

<head>
<SCRIPT TYPE="text/javascript">
<!--
function resubmit()
{
document.addanad.action="addad.php";
document.addanad.submit();
}
function process()
{
document.addanad.action="processad.php";
document.addanad.submit();
}
//-->
</script>
</head>

<body>
<form name="addanad" method="POST">
<input type="hidden" name="action" value="addad">
<table setup and other form fields here>
<tr>
<td>State:</td>
<td><select name="state" onchange="resubmit()">
<?php
$getstatesquery="SELECT DISTINCT loc_state FROM ads_location ORDER BY loc_state";
$getstates=mysql_query($getstatesquery) or die ("Could not acquire States list");
$statesnum=mysql_num_rows($getstates);
$count=0;
if ($value=="")
{
echo "<option selected value =\"\">Select a State</option>";
}
else
{
echo "<option selected value =\"$state\">$state</option>";
}
while ($states=mysql_fetch_row($getstates))
{
if ($value=="")
{
echo "<option>$states[0]</option>";
}
if (!$value=="")
{

echo "<option>$states[0]</option>";
$selected=$state;
}

}
$count++;
?>
</select></td>
</tr>
<tr>
<td>County:</td>
<td><select name="county">
<?php
if ( $value == "" )
{
$getcountiesquery="SELECT loc_county FROM ads_location WHERE loc_state='$init_state'";
$getcounties=mysql_query($getcountiesquery) or die ("Could not acquire county list!");
$counties=mysql_num_rows($getcounties);
echo $counties;
while ($counties=mysql_fetch_row($getcounties))
{
echo "<option>$counties[0]</option>";
}
} else {
$getcountiesquery2="SELECT loc_county FROM ads_location WHERE loc_state='$selected'";
$getcounties2=mysql_query($getcountiesquery2) or die ("Could not acquire county list!");
while ($counties2=mysql_fetch_row($getcounties2))
{
echo "<option>$counties2[0]</option>";
}
}
?>
</select></td>
</tr>
<other form fields, form closing, table closing and closing HTML here>

As I said, it's works great in FF, but doesn't trigger at all in IE.

Thanks *SO MUCH* for your help. I usually only do 'design' but I'm learning to do coding and development :)

[edited by: jatar_k at 5:36 pm (utc) on Jan. 27, 2005]
[edit reason] removed url [/edit]

jusdrum

5:33 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



Hello,
IE and Firefox have different ideas on their DOM (Document Object Model), which explains why it works in IE and not Firefox.

document.addanad.action="addad.php";
document.addanad.submit();

This is fine for IE, but for it to work in Firefox, you have to do a little checking in your function. Here's a rewrite that should work:

function resubmit()
{
if (document.all)
{
document.all.addanad.action="addad.php";
document.all.addanad.submit();
}
else
{
document.addanad.action="addad.php";
document.addanad.submit();
}
}

TWelch

5:41 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



Actually, it works in FF but not in IE.

But I will try your code just because I'm willing to try anything at this point :)

Edit: no change :(

jusdrum

5:51 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



Oh! I though the actual ACTION attribute of the form is what you wanted to change. You are wanting to change the value of a hidden form field. Well that's simple!

document.all.addanad.action.value="addad";

Since action might be a reserved word, you may want to change the name of that hidden form field to something like formaction.

TWelch

6:18 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



You are now my hero!

Birdman

7:36 pm on Jan 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[edit]this is a reply to:
>>Is there another way to do this using PHP without totally reloading the page on a select change?

glad it's working though
[edit]

Ok, here's how I would do it. I would forget the DB since the states and counties won't be changing. you could use the db but it's just added overhead to the system.

Simply hand code the states/counties array:


<?php
$states = array();
$states['Maryland'] = array('Anne Arundel', 'Montgomery', etc...);
$states['delaware'] = array('Sussex', etc...);
// and so on for all states
?>

Save that as states_counties.php(then you can include() it in the main script)

I would also recommend doing the same for the JavaScript arrays as well. This way, you can keep the JS in an external file and people who don't have it enabled won't have to download id. It will be a pretty good sized file.

So, now create the javascript file with the functions and arrays:


states = new Array();
states['Maryland'] = new Array('Anne Arundel', 'Montgomery', etc...);
states['delaware'] = new Array('Sussex', etc...);
// and so on for all states

// Now the function
function load_counties(state){

if(state == 'Select a state') return; // end if "select a state" chosen

var counties = '';
for(var i in states[state]){
counties += '<option>' + i + '</option>';
}

document.getElementById('county').innerHTML = counties;

}

Now save that as states_counties.js(for inclusion in the main script)

Now on to the main form script:


<?php
include('constants.php');
include('functions.inc');
include('states_counties.php');
$connect=connection($dbname);

if ( isset( $_POST['state'] ) && in_array( $_POST['county'], $states[$_POST['state']] ) ){
// at this point, I would redirect to a processing script since you now have the county selected
header('Location: process.php');
exit;
}

$states_opts = '';
$counties_opts = '';

if (!isset( $_POST['state'] ) ) {

$states_opts .= '<option>Select a State</option>';

} else {

//Build the counties options if state selected
foreach ( $states[$_POST['state']] as $county ) {
$counties_opts .= '<option>' . $county . '</option>';
}

}

//Build the states options
foreach ( $states as $state ) {

if ( isset( $_POST['state'] ) && $_POST['state'] == $state ) {
$selected = ' selected="selected"';
} else {
$selected = '';
}
$state_opts .= '<option' . $selected . '>' . $state . '</option>';
}

?>

<head>
<SCRIPT TYPE="text/javascript" src="states_counties.js"></script>
</head>

<body>
<form name="addanad" method="POST" action="<?php echo $_SERVER['PHP_SELF']">
<input type="hidden" name="action" value="addad">
<table setup and other form fields here>
<tr>
<td>State:</td>
<td><select name="state" id="state" onchange="load_counties(this.value)">
<?php
print $states_opts;
?>
</select></td>
</tr>
<tr>
<td>County:</td>
<td><select name="county" id="county">
<?php
print $counties_opts;
?>
</select></td>
</tr>
<other form fields, form closing, table closing and closing HTML here>

That's about it! Let me know how it goes.