...

View Full Version : Script not working in FF



tomraffe
01-27-2009, 04:45 PM
The validation script below works in IE but not in FF. Any suggestions?

Site: spendandgive.biz (http://www.spendandgive.biz)


<script>

function validate2(){

if(document.form1.shop.value == ''){
alert('Please select your shop before continuing!');
document.form1.shop.focus();
return false;
}
if(document.form1.charity.value == ''){
alert('Please select your charity before continuing');
document.form1.charity.focus();
return false;
}



}
</script>

tosbourn
01-27-2009, 04:59 PM
Try adding something like..



var form = document.getElementById('form1');


Then in place of document.form1....

form.charity.value ==''

Philip M
01-27-2009, 05:38 PM
What are shop and charity? Are they values obtained from textboxes, radio buttons, checkboxes or select lists?

Suggest you post your relevant HTML markup.

tomraffe
01-27-2009, 06:09 PM
Select lists.

Philip M
01-27-2009, 06:24 PM
I did ask you to post your relevant HTML mark-up.

The following works fine in both IE and Firefox, and I see nothing in what you have posted so-far to explain why you have a problem with FF.


<form name = "form1">
<select name = "shop">
<option value = "">Select a shop</option>
<option value = "Shop1">Shop1</option>
<option value = "Shop2">Shop2</option>
</select>

<select name = "charity">
<option value = "">Select a charity</option>
<option value = "Charity1">Charity1</option>
<option value = "Charity2">Charity2</option>
</select>

<input type = "button" name = "but1" value = "Validate" onclick = "validate2()">
</form>

<script type = "text/javascript">
function validate2(){

if(document.form1.shop.value == ''){
alert('Please select your shop before continuing!');
return false;
}
if(document.form1.charity.value == ''){
alert('Please select your charity before continuing');
return false;
}


}
</script>

tomraffe
01-27-2009, 07:36 PM
Sorry I didnt have access to the code at the time, but here is the select script. Check out spendandgive.biz and you will see it doesnt work in FF.


1. <select name='shop' class="bodytxt" size="1" id="shop_id" onChange="return select_server();">
<option>---Select your shop----</option>
<?php $sql = mysql_query("select * from ".SHOP." order by shop_name asc ") or die( mysql_error());
while($rs = mysql_fetch_array( $sql )){
?>
<option value="<?=$rs['shop_id']?>"><?=$rs['shop_name']?></option>
<?php
}
?>
</select>

</td>
<td><center>

2. <select name="charity" class="bodytxt" size="1">
<option>---Select your charity----</option>
<?php $sql = mysql_query("select * from ".CHARITY." order by charity_name asc ") or die( mysql_error());
while($rs = mysql_fetch_array( $sql )){
?>
<option value="<?=$rs['charity_id']?>"><?=$rs['charity_name']?></option>
<?php
}
?>
</select>

Philip M
01-27-2009, 07:56 PM
Agreed something weird is happening in FF.

Use some temporary alerts to help identify the problem:-

function validate2(){

alert (document.form1.shop.value);
alert (document.form1.charity.value);

if(document.form1.shop.value == ''){

Cranford
01-27-2009, 08:47 PM
@tomraffe
Validate your mark-up. You are missing the closing form tag, along with 66 other errors.

Place ALL script tags within the Head section. JavaScript has no place inside the Body. None.

http://validator.w3.org/

http://jigsaw.w3.org/css-validator/

P.S.

This: http://www.tizag.com/javascriptT/javascriptheadnbody.php

is garbage. It wasn't valid in 2003 when the site was started and it isn't valid today. Garbage, dressed up in arrogance and pretension. Garbage.

Philip M
01-27-2009, 09:03 PM
Place ALL script tags within the Head section. JavaScript has no place inside the Body. None.


For a less dogmatic opinion see:-
http://www.tizag.com/javascriptT/javascriptheadnbody.php

The location choice of head or body is very simple. If you want to have a script run on some event, such as when a user clicks somewhere, then you will place that script in the head. If you want the script to run when the page loads, like our "Hello World!" example in the previous lesson, then you will want to place the script within the body tag.


JavaScripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, other times when a user triggers an event.
Scripts in the head section: Scripts to be executed when they are called, or when an event is triggered, go in the head section. When you place a script in the head section, you will ensure that the script is loaded before anyone uses it.
Scripts in the body section: Scripts to be executed when the page loads go in the body section. When you place a script in the body section it generates the content of the page.
Scripts in both the body and the head section: You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum