Jump to content

My script doesn't work if I add break keyword inside foreach loop for php


sunny124

Recommended Posts

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>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.