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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.