Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    array items undefined some of the time

    I have a function that iterates through an array checking for values and changing them based on what it finds.

    There is a mix of php creating tabular layout of info and javascript to repopulate a javascript array based on what the peice of data the user selects from the tabular format.

    The first go through of the tabular layout with php using a foreach loop will work for javascript just fine. although the rest will cause the array in javascript to have undefined data.

    If I simply alert the array no matter what peice of data is selected it works fine. If I iterate through the array in a for loop though it will return undefined data.

    Here is my javascript array as I define it:
    Code:
    contractArray = new Array();
    contractString = '<?php echo $_SESSION['contracts']; ?>'
    contractArray = contractString.split(',');
    alert(contractArray); // alert works fine
    function that is run when user selects item from php tabular layout (this works fine when selecting the first item in the php table)
    Code:
    function addContract(newContract, oldContract)
    {
    	// add contract to cart
    	count = contractArray.length;
    	alert(contractArray); // always works 
    	indexes = count - 1;
    	if(count == 0)
    	{
    		contractArray[0] = newContract;
    	}
    	else
    	{
    		for(i=0;i<count;i++)
    		{
    			if(contractArray[i] == oldContract)
    			{
    				alert('old: '+contractArray[i]+' : '+newContract);
    // contractArray[i] returns undefined on all but first php table
    				contractArray[i] = newContract;
    				break;
    			}
    			else if(contractArray[i] == newContract)
    			{
    				alert('new: '+contractArray[i]+' : '+newContract); // contractArray[i] returns undefined on all but first php table
    				break;
    			}
    			else
    			{
    				alert('add: '+contractArray[count]+' : '+newContract); // contractArray[count] returns undefined on all but first php table
    				contractArray[count] = newContract;
    				break;
    			}
    		}
    	}
    
    	alert(contractArray); // always works
    // all other variables and arguments in function always work
    	return contractArray;
    
    }

  • #2
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    discovered some interesting info.

    During my addContract function I noticed that what was failing was the if tests to use the i iterator for checking if values match in the array.

    If I use a normal int as the index identifier it works great.

    I did do a check and alerted the i variable to see that it is working properly and it is. I am also able to alert() an index value from the array with the i variable just fine, just doesnt work in the if test


    Any thoughts

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,559 Times in 4,523 Posts
    Can you give us the URL to a live page with the problem?

    If not then do this:
    -- bring up the problem page in your browser.
    -- click the browser's VIEW menu
    -- click the SOURCE or PAGE SOURCE menu item
    -- the browser's view of your generated HTML (that is, what PHP created) will show in another window or editor
    -- copy/paste *THAT* code here
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks for the reply, here is the page source:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Home | Free website template from TemplateMonster.com</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <meta name="description" content="Home page - free business website template available at TemplateMonster.com for free download."/>
    <link href="style/style.css" rel="stylesheet" type="text/css" />
    <link href="style/layout.css" rel="stylesheet" type="text/css" />
    <!--<script src="script/maxheight.js" type="text/javascript"></script>-->
    <!--[if lt IE 7]>
    	<link href="ie_style.css" rel="stylesheet" type="text/css" />
    <![endif]-->
    </head>
    <body id="page1" onload="new ElementMaxHeight();">
    <!-- header -->
    	<div id="header">
    		<div class="container">
    			<div class="row-1">
    				<div class="logo"><a href="index.html"><img alt="" src="images/logo.jpg" /></a></div>
    				<ul class="top-links">
    					<li><a href="index.php"><img alt="" src="images/top-icon1.jpg" /></a></li>
    					<li><a href="#"><img alt="" src="images/top-icon2.jpg" /></a></li>
    					<li><a href="contact-us.php"><img alt="" src="images/top-icon3.jpg" /></a></li>
    				</ul>
    			</div>
    			<div class="row-2">
    <!-- nav box begin -->
    				<div class="nav-box">
    					<div class="left">
    						<div class="right">
    							<ul>
    								<li><a href="index.php" class="first-current"><em><b>HOME</b></em></a></li>
    								<li><a href="products.php"><em><b>PRODUCTS</b></em></a></li>
    								<li><a href="cart.php"><em><b>CART</b></em></a></li>
    								<li><a href="login.php"><em><b>LOGIN</b></em></a></li>
    								<li><a href="view-checkout.php"><em><b>CHECKOUT</b></em></a></li>
    								<li><a href="contact-us.php" class="last"><em><b>CONTACT US</b></em></a></li>
    							</ul>
    						</div>
    					</div>
    				</div>
    <!-- nav box end -->
    			</div>
    		</div>
    	</div><!-- content -->
    	
    	
    	<div id="content">
    	<div class="ic"></div>
    	
    	
    		<div class="container">
    			<!-- main-banner-small begin -->
    	<a href="javascript:void(0)" onclick="continueShopping()" ><img class="order_cart_buttons" alt="" src="images/continue_shopping.png" /></a>
    			
    	<a href="javascript:void(0)" onclick="checkOrder()"><img class="fright order_cart_buttons" alt="" src="images/buy-now-button.png" /></a>
    	<div class="clear"></div>	
    			
    <!-- main-banner-small end -->
    			<div class="section">
    <!-- box begin -->
    				<div class="box">
    					<div class="border-top">
    						<div class="border-right">
    							<div class="border-bot">
    								<div class="border-left">
    									<div class="left-top-corner">
    										<div class="right-top-corner">
    											<div class="right-bot-corner">
    												<div class="left-bot-corner">
    													<div class="inner">
    													
    <h1 style="color: #404141;">Music Editor 3</h1>
    <img class="img-indent alt software_box_detail" alt="" src="images/music-boxshot.png" />
    <div class="feature_order_container">
    	
    		<ul class="product_desc "><li>Over 15 different transitions</li><li>Export in multiple formats</li><li>Supports unlimited channels</li><li>Filter music through processors</li><li>Easy to use</li>
    		</ul>
    
    	<div class="order_buttons">
    	<a href="javascript:void(0)" onmouseup="addContract('2133786', '2131572')">
    		<div class="order_button_group" style="margin: 10px 0px;" id="2133786">
    			<h2>One Time Purchase</h2>
    			<h1>$29.99</h1>
    			
    		</div>
    	</a>
    	<hr />
    	<a href="javascript:void(0)" onmouseup="addContract('2131572', '2133786')">
    		<div class="order_button_group" id="2131572">
    			<h2>Monthly Subscription</h2>
    			<h1 >$9.99</h1>
    			
    		</div>
    	</a>
    	</div>
    	
    </div>
    	<!--
    	<div class="purchase_info">
    		<p>You can purchase Music Editor 3 for a single price or you can sign up for a subscription and recieve free updates and lifetime access to the latest versions for a low monthly price.</p>
    	</div>
    	-->
    <div class="clear"></div>
    
    
    														<div class="clear"></div>
    													</div>
    												</div>
    											</div>
    										</div>
    									</div>
    								</div>
    							</div>
    						</div>
    					</div>
    				</div>
    <!-- box end -->
    			</div>	
    <!-- box end -->
    		</div>
    	</div>
    	
    	
    <!-- extra-content -->
    
    
    <script type="text/javascript">
    
    contractArray = new Array();
    contractString = '<br />
    <b>Notice</b>:  Undefined index: contracts in <b>/var/www/html/mockups/demo-soft/cart.php</b> on line <b>258</b><br />
    '
    contractArray = contractString.split(',');
    
    	count = contractArray.length;
    	for(i=0; i<count; i++)
    	{
    
    		document.getElementById(contractArray[i]).className = 
    	   document.getElementById(contractArray[i]).className.replace
    		  ( /(?:^|\s)order_button_group(?!\S)/ , 'order_button_group_clicked' );
    		
    	}
    	alert(contractArray);
    
    
    
    function addContract(newContract, oldContract)
    {
    	// add contract to cart
    	count = contractArray.length;
    	alert(contractArray+'--'+contractArray[2]);
    	indexes = count - 1;
    	if(count == 0)
    	{
    		contractArray[0] = newContract;
    	}
    	else
    	{
    		for(i=0;i<count;i++)
    		{
    			if(contractArray[1] == oldContract)
    			{
    				alert('old: '+i+'-'+contractArray[i]+' : '+newContract);
    				contractArray[i] = newContract;
    				break;
    			}
    			else if(contractArray[1] == newContract)
    			{
    				alert('new: '+i+'-'+contractArray[i]+' : '+newContract);
    				break;
    			}
    			else
    			{
    				alert('add: '+count+'-'+contractArray[count]+' : '+newContract);
    				contractArray[count] = newContract;
    				break;
    			}
    		}
    	}
    	
    	//change class names of order_button_groups
    	
    
    	altClass = document.getElementById(oldContract).className;
    	if(altClass == 'order_button_group_clicked')
    	{
    		document.getElementById(oldContract).className = 
    		document.getElementById(oldContract).className.replace
    		( /(?:^|\s)order_button_group_clicked(?!\S)/ , 'order_button_group' );
    	}
    	
    	  
    	document.getElementById(newContract).className = 
       document.getElementById(newContract).className.replace
          ( /(?:^|\s)order_button_group(?!\S)/ , 'order_button_group_clicked' );
    	alert(contractArray+'--'+contractArray[1]);
    	return contractArray;
    	
    	//alert(count+', '+contractArray+', '+keep+', '+discard);
    	//alert('altClass: '+altClass+', alt: '+alt+', type: '+type+', id: '+id);
    }
    
    function checkOrder(contractArray)
    {
    alert(contractArray);
    	productCount = 1;
    	contractCount = contractArray.length;
    	// if(contractCount > 0 && productCount == contractCount)
    	// {
    		contractArray.toString();
    		location.href = 'login.php?contracts='+contractArray;
    	// }
    	// else
    	// {
    		// alert('Please select One Time Purchase, or Monthly Subscription');
    	// }
    }
    
    function continueShopping(contractArray)
    {
    	contractCount = contractArray.length;
    	if(contractCount > 0)
    	{
    		contractArray.toString();
    		location.href = 'products.php?contracts='+contractArray;
    	}
    	else
    	{
    		location.href = 'products.php';
    	}
    }
    
    </script>
    	
    <!-- footer -->
    		<div id="footer">
    		<div class="container">
    			<ul class="nav">
    				<li><a href="index.php">Home</a>|</li>
    				<li><a href="about-us.php">About Us</a>|</li>
    				<li><a href="solutions.php">Solutions</a>|</li>
    				<li><a href="partners.php">Partners</a>|</li>
    				<li><a href="consulting.php">Consulting</a>|</li>
    				<li><a href="contact-us.php">Contact Us</a></li>
    			</ul>
    			<div class="wrapper">
    				<div class="fleft">Copyright &copy; 2009</div>
    			</div>
    		</div>
    	</div></body>
    </html>

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,559 Times in 4,523 Posts
    Ummm...okay...and how do I make it cause the problem you are talking about?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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