SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict Caryn's Avatar
    Join Date
    Aug 2002
    Location
    Sydney, Australia
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined variable

    Hi,

    I'm a shocker at javascript - have absolutely no idea.

    I'm having a problem where a script checks radio buttons and if none are checked, then it alerts the user to choose one.

    The error that I'm receiving is that 'nonechecked' is undefined. This is in a php page where the compusory radio button in question may total only 1 at times, this is when the error has started occurring.

    Code:
    for (a=0; a < fields.length; ++a)
    	{
    		for (f=0; f < eval("document.form1." + fields[a][0] + ".length"); ++f){
    			if (eval("document.form1." + fields[a][0] + "[f].checked == true")){
    				nonechecked = 0;
    				break;
    			}
    			else
    				{nonechecked = 1;}	
    		}
    		if (nonechecked == 1){
    			req[req.length] = fields[a][1] + "\n";
    		}	
    	}
    The error refers to the line that says "if(nonechecked ==1)".

    Is anybody able to point me in the right direction?

    Thanks,

    Caryn

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well,

    You should declare the variable outside the loop. But the main reason you're having trouble is because you have the ++ on the wrong side of the letter. It should be a++ and f++

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Prefixing (++a) or postfixing (a++) is not an issue as nothing is done with the counter other than incrementing it. The use of eval() is almost always unnecessary (...f < document.form1[fields[a][0]].length;...)

    Without your HTML, this is just a guess...
    Code:
    function checkRadios(radgrp) //e.g., document.form1.radio_name
    {
    	if (typeof radgrp[0] == 'undefined') //singlet
    		radgrp = [radgrp];
    	var i = radgrp.length;
    	do
    		if (radgrp[--i].checked)
    			return false;
    	while (i);
    	req[req.length] = fields[a][1] + "\n"; //change this to...?
    }
    Who knows what those variables are?
    Last edited by adios; May 24, 2004 at 09:41.
    ::: certified wild guess :::

  4. #4
    SitePoint Addict Caryn's Avatar
    Join Date
    Aug 2002
    Location
    Sydney, Australia
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was stupid of me not to post the full code of the form in the first place. I've left out the first PHP code because I don't think it's relevant, but let me know if it is:

    Code:
    <form method="post" action="" name="form1" onsubmit="return FormValidator()">
    <input type="hidden" name="chapterid" value="<? echo($chapterid)?>">
    <table border="0" cellspacing="2" cellpadding="2">
    	<tr>
    		<td><b><!-- In date order -->Undistributed Newsletters</b></td>
    		<td><b>Chapter Name</b></td>
    		<td><b>Send</b></td>
    	</tr>
    	<?
    	if($result) 
    	{
    		for($count=0; $date_rows = mysql_fetch_row($result);$count++)
    		{
    			$date = substr($date_rows[2],8,2)."/".substr($date_rows[2],5,2)."/".substr($date_rows[2],2,2);   
    	?>
    	<tr>
    		<td><? echo($date)?></td>
    		<td><? echo(get_chapter_name($date_rows[1]))?></td>
    		<td><input type="radio" name="sendNewsLetter" value="<? echo($date_rows[0])?>"></td>
    	</tr>
    	<?
    		}
    	}
    	?>
    	
    	<tr>
    		<td colspan="3"><b>Extra Email Addresses to Send to:</b></td>
    	</tr>
    	<tr>
    		<td colspan="3"><input class="plain" type="text" name="extraEmail1"></td>
    	</tr>
    	<tr>
    		<td colspan="3"><input class="plain" type="text" name="extraEmail2"></td>
    	</tr>
    	<tr>
    		<td colspan="3"><input class="plain" type="text" name="extraEmail3"></td>
    	</tr>
    	<tr>
    		<td colspan="3"><input class="plain" type="text" name="extraEmail4"></td>
    	</tr>
    	<tr>
    		<td colspan="3"><input class="plain" type="text" name="extraEmail5"></td>
    	</tr>
    	<tr>
    		<td colspan="3"><input class="plain" type="text" name="extraEmail6"></td>
    	</tr>
    	<tr>
    		<td colspan="3"><input class="plain" type="text" name="extraEmail7"></td>
    	</tr>
    	<tr>
    		<td colspan="3"><input class="plain" type="text" name="extraEmail8"></td>
    	</tr>
    	<tr>
    		<td colspan="3"><input class="plain" type="text" name="extraEmail9"></td>
    	</tr>
    	<tr>
    		<td colspan="3"><input class="plain" type="text" name="extraEmail10"></td>
    	</tr>
    	<tr>
    		<td colspan="3"><input class="submit" type="submit" name="send" value="Send"></td>
    	</tr>
    </table>
    </form>
    <script language="JavaScript">
    function FormValidator()
    {
    
    	var fields = new Array(1);
    	// Here you can have a custom response for the missing fields
    	fields[0] = new Array("sendNewsLetter","Please choose a newsletter to send.");
    	
    	req = new Array(); //req is a container for missing fields
    	
    	for (a=0; a < fields.length; ++a)
    	{
    		for (f=0; f < eval("document.form1." + fields[a][0] + ".length"); ++f){
    			if (eval("document.form1." + fields[a][0] + "[f].checked == true")){
    				nonechecked = 0;
    				break;
    			}
    			else
    				{nonechecked = 1;}	
    		}
    		if (nonechecked == 1){
    			req[req.length] = fields[a][1] + "\n";
    		}	
    	}
    
    	if (req.length != 0){
    		for (x=0; x < req.length; ++x){
    			missing = req[x];
    		}		
    		// Display the alert	
    		
    		alert(missing);
    		return false;	
    	}
    
        return (true);
    }
    
    </script>
    Cheers.

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function FormValidator()
    {
    	var radgrp = document.form1.sendNewsLetter;
    	if (typeof radgrp[0] == 'undefined') //singlet
    		radgrp = [radgrp];
    
    	var i = radgrp.length;
    	do
    		if (radgrp[--i].checked)
    			return true;
    	while (i);
    
    	alert('Please choose a newsletter to send.');
    	radgrp[0].focus();
    	return false;	
    }
    Last edited by adios; May 25, 2004 at 10:00.
    ::: certified wild guess :::


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •