Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > undefined field error message

Reply
Thread Tools

undefined field error message

 
 
Xerxes
Guest
Posts: n/a
 
      02-27-2006
Hi,
I have a script that is supposed to check if one of the radio buttons
in a form is checked and either display an "alert" or go to another
page.

<html>
<head>
<title></title>
</head>
<body>
<script type="text/Javascript">
function getRadioButtonChecked (radio){
for (var i = 0; i < radio.length; i++){
if (radio[i].checked){
return true;
}
}
return false;
}
</script>

<form name="myform" method="post" action="somefile.php">
<input id="req_any" name="req_any" type="radio" value="AVM-MC1"
onClick="if (this.checked)
document.getElementById('qty_AVM-MC1').value='1'; else
document.getElementById('qty_AVM-MC1').value='0'">Product1<br />
<input type="hidden" id="qty_AVM-MC1" name="qty_AVM-MC1" value="0">
<input id="req_any" name="req_any" type="radio" value="AVM-HTC1"
onClick="if (this.checked)
document.getElementById('qty_AVM-HTC1').value='1'; else
document.getElementById('qty_AVM-HTC1').value='0'">Product2<br />
<input type="hidden" id="qty_AVM-HTC1" name="qty_AVM-HTC1" value="0">
<br />
<span style="margin-left:40%;">
<input name="OK" type="submit" value="Add" onClick="if
(getRadioButtonChecked(document.getElementById('re q_any'))){document.location.href='anotherfile.php' ;
return true;} else{ alert('You must select one of the required
options.'); return false;}">
</span>
<span style="margin-left:50;">
<input name="Cancel" type="button" value="Cancel"
onClick="document.location.href='cancel.php'">
</span>
</form>
</body>

When I click on the "Add" button, I get a javascript error saying
"radio is not defined". What am I doing wrong?

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      02-27-2006
Xerxes said:
>
>Hi,
>I have a script that is supposed to check if one of the radio buttons
>in a form is checked and either display an "alert" or go to another
>page.
>
><html>
><head>
><title></title>
></head>
><body>
><script type="text/Javascript">
>function getRadioButtonChecked (radio){
> for (var i = 0; i < radio.length; i++){
> if (radio[i].checked){
> return true;
> }
> }
> return false;
>}
></script>
>
><form name="myform" method="post" action="somefile.php">
><input id="req_any" name="req_any" type="radio" value="AVM-MC1"
>onClick="if (this.checked)
>document.getElementById('qty_AVM-MC1').value='1'; else
>document.getElementById('qty_AVM-MC1').value='0'">Product1<br />
><input type="hidden" id="qty_AVM-MC1" name="qty_AVM-MC1" value="0">
><input id="req_any" name="req_any" type="radio" value="AVM-HTC1"
>onClick="if (this.checked)
>document.getElementById('qty_AVM-HTC1').value='1'; else
>document.getElementById('qty_AVM-HTC1').value='0'">Product2<br />
><input type="hidden" id="qty_AVM-HTC1" name="qty_AVM-HTC1" value="0">
><br />
><span style="margin-left:40%;">
><input name="OK" type="submit" value="Add" onClick="if
>(getRadioButtonChecked(document.getElementById('r eq_any'))){document.location.href='anotherfile.php ';
>return true;} else{ alert('You must select one of the required
>options.'); return false;}">
></span>
><span style="margin-left:50;">
><input name="Cancel" type="button" value="Cancel"
>onClick="document.location.href='cancel.php'">
></span>
> </form>
></body>
>
>When I click on the "Add" button, I get a javascript error saying
>"radio is not defined". What am I doing wrong?


Arranged roughly from serious to minor:

1. You've got two elements with the same id, which is illegal.
2. The action of your form specifies one URL, but the onclick handler of your
submit button specifies another. Maybe you just forgot which bogus name you
were using?
3. You're using getElementById() to get a reference to an element in the same
form.
4. You're validating in the onclick handler of the submit button instead of in
the onsubmit handler of the form.
5. You're including too much Javascript in-line with your HTML instead of in
functions in a <script> block.
6. You've got a script block in the body that would be better in the head.

<html>
<head>
<title></title>
<script type="text/Javascript">
function getRadioButtonChecked (radio){
for (var i = 0; i < radio.length; i++){
if (radio[i].checked){
return true;
}
}
alert('You must select one of the required options.');
return false;
}
</script>
</head>
<body>
<form name="myform"
method="post"
action="somefile.php"
onsubmit="return getRadioButtonChecked(req_any)">
<input name="req_any"
type="radio"
value="AVM-MC1"
onclick="this.form.qty_AVM_MC1=this.checked?1:0">P roduct1<br />
<input type="hidden"
id="qty_AVM-MC1"
name="qty_AVM-MC1"
value="0">
<input name="req_any"
type="radio"
value="AVM-HTC1"
onclick="this.form.qty_AVM_HTC1=this.checked?1:0"> Product2<br />
<input type="hidden"
id="qty_AVM-HTC1"
name="qty_AVM-HTC1"
value="0">
<br />
<span style="margin-left:40%;">
<input name="OK"
type="submit"
value="Add">
</span>
<span style="margin-left:50;">
<input name="Cancel"
type="button"
value="Cancel"
onClick="document.location.href='cancel.php'">
</span>
</form>
</body>
</html>

 
Reply With Quote
 
 
 
 
Xerxes
Guest
Posts: n/a
 
      02-28-2006
Hi Lee,
thanks a lot for the info.
The page is generated dynamically through a php file. I made the
changes you suggested and thsi the resuting page, which seems to
satisfy your suggestions. Thejavascript console still shows the same
error message: "error: radio is not defined".

<html>
<head>
<title></title>
<script type="text/Javascript">
function getRadioButtonChecked (radio){
for (var i = 0; i < radio.length; i++){
if (radio[i].checked){
alert(radio+i+' is checked');
return true;
}
}
alert("You must select one of the required options.");
return false;
}
</script>
</head>

<body topmargin="70">
<form name="error_form" method="post" action="add_additional.php"
onsubmit="return getRadioButtonChecked('req_any')">
<div class="msgDiv">
<div width="300" class="contentMsgHeader">
Error message is printed here...<br />
</div>
<span class="contentMsg">
<input class="errorFormInput"
name="req_any"
type="radio"
value="AVM-MC1"
onClick="this.form.qty_AVM-MC1.value=if
(this.checked)?1:0">Prodcut1</span><br />
<input type="hidden"
id="qty_AVM-MC1"
name="qty_AVM-MC1" value="0">
<span class="contentMsg">
<input class="errorFormInput"
name="req_any"
type="radio"
value="AVM-HTC1"
onClick="this.form.qty_AVM-HTC1.value=if
(this.checked)?1:0">Prodcut2</span><br />
<input type="hidden"
id="qty_AVM-HTC1"
name="qty_AVM-HTC1"
value="0">
</div>
</div>
<br /><span style="margin-left:40%;">
<input name="OK"
type="submit"
value="Add Additional">
</span>
<span style="margin-left:50;">

<input name="Cancel"
type="button"
value="Cancel"
onClick="document.location.href='cancel_addition.p hp'">
</span>
</form>
</body>

Thanks for your help.

 
Reply With Quote
 
web.dev
Guest
Posts: n/a
 
      02-28-2006

Xerxes wrote:
> Hi Lee,
> thanks a lot for the info.
> The page is generated dynamically through a php file. I made the
> changes you suggested and thsi the resuting page, which seems to
> satisfy your suggestions. Thejavascript console still shows the same
> error message: "error: radio is not defined".


The error message is correct.

> function getRadioButtonChecked (radio){
> for (var i = 0; i < radio.length; i++){
> if (radio[i].checked){
> alert(radio+i+' is checked');
> return true;
> }
> }
> alert("You must select one of the required options.");
> return false;
> }


You have an input element with the value of "req_any" for the name
attribute. When the form gets submitted, you pass a _string_ to your
function getRadioButtonChecked. However, in your function, you're
trying to use "radio" as an object, when in reality you passed to it a
string. Try modifying your code like so:

javascript:

function getRadioButtonChecked(myForm)
{
for(var i = 0; i < myForm.elements["req_any"].length; i++)
{
if(myForm.elements["req_any"][i].checked)
{
alert("radio " + i + " is checked");

return true;
}
}

alert("You must select one of the required options.");

return false;
}

html:

<form [...add form attributes...] onsubmit = "return
getRadioButtonChecked(this)">
[...add your form controls...]
</form>

 
Reply With Quote
 
Xerxes
Guest
Posts: n/a
 
      02-28-2006
Thanks alot for your help. Your suggestion worked.

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      02-28-2006
Xerxes said:
>
>Hi Lee,
>thanks a lot for the info.
>The page is generated dynamically through a php file. I made the
>changes you suggested and thsi the resuting page, which seems to
>satisfy your suggestions. Thejavascript console still shows the same
>error message: "error: radio is not defined".
>
><html>
><head>
><title></title>
><script type="text/Javascript">
>function getRadioButtonChecked (radio){
> for (var i = 0; i < radio.length; i++){
> if (radio[i].checked){
> alert(radio+i+' is checked');
> return true;
> }
> }
> alert("You must select one of the required options.");
> return false;
>}
></script>
></head>
>
><body topmargin="70">
><form name="error_form" method="post" action="add_additional.php"
>onsubmit="return getRadioButtonChecked('req_any')">


Remove the quotes from around req_any.
You want to pass a reference to the form element req_any,
not the name of the element.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
typeof x == 'undefined' or x == undefined? -Lost Javascript 13 01-31-2007 12:04 AM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments