sunny124 Posted January 13, 2011 Share Posted January 13, 2011 Hi guys, why is my script not working if i put a break inside a php foreach loop. I have two functions inside the javascript and none of the functions work. If I remove the break keyword, only the first function works. The second function keeps going to the else even if the if condition inside the foreach loop is true. <script type="text/javascript"> function display_Price_if_chosen_from_drop_down(key) { var qty_dropdown; qty_dropdown = document.getElementById('SizeOrQuantity'+key); document.getElementById('Price'+key).value = qty_dropdown.options[qty_dropdown.selectedIndex].getAttribute("cost"); } function display_price_if_quantity_entered(key) { var qty_textfield; qty_textfield = document.getElementById("Quantity"+key).value; <?php foreach($product_special_key as $row){?> //alert(<?//=$row["Quantity"]?>); if(qty_textfield==<?=$row["Quantity"]?>) { alert("quantity "+<?=$row["Quantity"]?>+" Price "+<?=$row["Price"]?>); document.getElementById('Price'+key).value = <?=$row["Price"]?>; <?break;?> } else { document.getElementById('Price'+key).value = (qty_textfield *<?=$StandardPrice?>); } <? } ?> } </script> Quote Link to comment Share on other sites More sharing options...
Omirion Posted January 13, 2011 Share Posted January 13, 2011 dude... that code is horrible... put some comments on it or something... Quote Link to comment Share on other sites More sharing options...
sunny124 Posted January 13, 2011 Author Share Posted January 13, 2011 sorry about that. Added comments now. <script type="text/javascript"> /* * below function executes if user chooses quantity from drop down box */ function display_Price_if_chosen_from_drop_down(key) { /* *assigns the price stored in custom attribute 'cost' to price textfield *cost is the custom attribute where the price from the array is stored */ var qty_dropdown; qty_dropdown = document.getElementById('SizeOrQuantity'+key); document.getElementById('Price'+key).value = qty_dropdown.options[qty_dropdown.selectedIndex].getAttribute("cost"); } /* * below function executes if user enters quantity in textfield */ function display_price_if_quantity_entered(key) { var qty_textfield = document.getElementById("Quantity"+key).value; /* * searchs to see if quantity entered exits in the array, * if the quantity does exist, the value for the price textfield is same as price in array * if quantity does not exist, price value is quantity multiplied by standard price */ <?php foreach($product_special_key as $row){?> //alert(<?//=$row["Quantity"]?>); if(qty_textfield==<?=$row["Quantity"]?>) { alert("quantity "+<?=$row["Quantity"]?>+" Price "+<?=$row["Price"]?>); document.getElementById('Price'+key).value = <?=$row["Price"]?>; <?break;?> } else { document.getElementById('Price'+key).value = (qty_textfield *<?=$StandardPrice?>); } <? } ?> } </script> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.