php - Javascript function not being called from onchange (not always) -
at moment, i'm in process of creating website, have products have quantity. quantity depends on size , color.
so came following procedure check , give feedback how many there still left.
<div class="product_stock" id="stock_form<?php echo $i; ?>"><?php $size = $product_sizes[0]; $color = $product_colors[0]; $sql = "select * products product_id = '$product_id' , size = '$size' , color = '$color'"; $result = mysql_query($sql) or die(mysql_error()); echo $sql; if(mysql_num_rows($result) > 0) { $product_stock = mysql_fetch_array($result); if($product_stock['stock'] > 0) { echo "nog ".$product_stock['stock']." verkrijgbaar"; } else { echo "combinatie niet meer verkrijgbaar"; } }?> </div> <div class="product_text"> <?php echo $product['text']; ?> </div> <div class="product_num"> art: #<?php echo $product['product_id']; ?> </div> <div class="product_bar_bot"> <form name="form<?php echo $i; ?>" action="shopping_cart.php?"> <input type="hidden" name="id" value="<?php echo $product['pid']; ?>"/> <select name="size" class="product_select" onchange="get_stock('form<?php echo $i; ?>');"> <?php foreach($product_sizes $p_size) { ?> <option> <?php echo $p_size; ?> </option> <?php } ?> </select> <select name="color" class="product_select" onchange="get_stock('form<?php echo $i; ?>');"> <?php foreach($product_colors $p_color) { ?> <option value="<?php echo $p_color; ?>"> <?php echo $color_array[$p_color]; ?> </option> <?php } ?> </select> <input type="hidden" name="url" value="sale"> <input type="hidden" name="action" value="add"> <input type="text" name="amount" value="1" size="1" style="vertical-align: top; margin-top: 9px;"></input> <img src="images/icons/cart_shop.png" align="top"/> <input type="submit" class="button" value="voeg toe" style="vertical-align: top; margin-top: 7px;">
the function required called:
function get_stock(formid) { var form = document.forms[formid]; var size = form.size.value; var color = form.color.value; var pid = form.id.value; var stock = 'stock_'+formid; // code ie7+, firefox, chrome, opera, safari if (window.xmlhttprequest) { xmlhttp=new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid(stock).innerhtml=xmlhttp.responsetext; } } xmlhttp.open("get","get_stock.php?id="+pid+"&size="+size+"&color="+color,true); xmlhttp.send(); } the problem i'm having work of forms, forms get_stock.php page isn't being called.
if more information needed don't hesitate ask.
i made small error fixed, @bergi's comment. creating forms adding +1 @ $i each time, reseted $i @ wrong place, resulted in more 1 of $i = 1 example.
Comments
Post a Comment