...

View Full Version : array items undefined some of the time



surreal5335
06-26-2012, 09:20 PM
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:


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)


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;

}

surreal5335
06-27-2012, 12:38 AM
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

Old Pedant
06-27-2012, 12:44 AM
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

surreal5335
06-27-2012, 06:53 PM
Thanks for the reply, here is the page source:



<!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>

Old Pedant
06-27-2012, 10:03 PM
Ummm...okay...and how do I make it cause the problem you are talking about?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum