#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    501
    Rep Power
    49

    Quantity Limit On Order Form


    I have a web store order form which has been working fine. Now I am trying to impose a limit on quantity so that a customer can not order more than a certain amount of a product. Each product might have a different limit (integer) or none at all (limit is set to zero).

    The code I have is as follows:
    Get the product information from PHP/MySQL:
    PHP Code:
    $sql "SELECT products.*...

    WHILE (
    $newArray = mysql_fetch_array($result)) {
            
    $product_id  = $newArray['product_id'];
            
    $product_name  = $newArray['product_name'];
            
    $prod_class = $newArray['prod_class'];
            
    $customer_max = $newArray['customer_max'];...
        if(
    $customer_max > 0){
            
    $max_note = "<br/> &nbsp &nbsp &nbsp &nbsp (Max: <span id=\"customer_max\">$customer_max</span>)";
        }else{
            
    $max_note "";
        }

    /* Create a line in the order form
     $order_form .="
             <TR>  
                <TD align=\"right\">  
                  $product_name $max_note $mandatory_note         
                </TD>  
                <TD>   
                  <INPUT type=\"text\" name=\"ttl_units[$i]\" class=\"unitCount\" data-id=\"$i\" data-standard=\"$standard_price\" data-member=\"$member_price\" size=\"10\" maxlength=\"10\" value=\"0\" /> 
                  <INPUT type=\"hidden\" name=\"prod_id[$i]\" value=\"$product_id\" /> 
                  <INPUT type=\"hidden\" name=\"prod_name[$i]\" value=\"$product_name\" /> 
                  <INPUT type=\"hidden\" name=\"price_type_label[$i]\" value=\"$price_type_label\" /> 
                  <INPUT type=\"hidden\" name=\"customer_max[$i]\" value=\"$customer_max\" /> 
                  <INPUT type=\"hidden\" name=\"req_boat_list[$i]\" value=\"$requires_boat_list\" /> 
                  <INPUT type=\"hidden\" name=\"unit_price[$i]\" id=\"unit_price[$i]\" value=\"0\" /> 
                  <INPUT type=\"hidden\" name=\"subttl[$i]\" class=\"subVal\" id=\"subttl[$i]\" value=\"0\" /> 
                 ($price_type_label)
                </TD>  
                <TD>   
                  <SPAN id=\"unitPriceDisp$i\"> $0 </SPAN> 
                </TD>  
                <TD>   
                  <SPAN id=\"unitSubDisp$i\"> $0  </SPAN> 
                </TD> 
              </TR>  
    ";
        $i++;   

    Then, my Javascript looks like the following:
    PHP Code:
        <script type="text/javascript">  
            $(
    '.unitCount').keyup(function(e){ 
                var 
    mbr_add Number(document.getElementById("mbr_add").innerHTML);
                var 
    curr_mbr "$member";
                if(
    curr_mbr == "yes" || mbr_add >0){
                    var 
    memType "member";
                }else{
                    var 
    memType "standard";
                }
                var 
    unitCount = $(this).val();
                var 
    unitSubId = $(this).attr('data-id'); 
                    
    alert ("ID: " unitSubId);        

                var 
    limit Number(document.getElementById('customer_max').innerHTML);
                    
    alert ("Limit: " limit);        
                if(
    unitCount limit){
                    
    unitCount limit;
                }
                var 
    unitPrice = {'standard': +$(this).attr('data-standard'), 'member': +$(this).attr('data-member'), 'joining': +$(this).attr('data-member')};  
                
    // var memType = document.getElementById("memSelect").value; 
                
    var memFee mbr_add;
                    
    // alert ("Mbr Fee: " + memFee);        
                
    var grandTotal 0.00
                var 
    unitSub = (unitCount unitPrice[memType]);
                
    document.getElementById('unitPriceDisp'+unitSubId).innerHTML '$'+unitPrice[memType].toFixed(2); 
                
    document.getElementById('unitSubDisp'+unitSubId).innerHTML '$'+unitSub.toFixed(2); 
                
    document.getElementById('unit_price['+unitSubId+']').value unitPrice[memType].toFixed(2); 
                
    document.getElementById('subttl['+unitSubId+']').value unitSub.toFixed(2); 
                $(
    '.subVal').each(function() { grandTotal += +this.value }); 
                if(
    memFee 0) {
                    
    grandTotal += memFee
                    
    document.getElementById('MemberFeeDisp').innerHTML '$'+memFee.toFixed(2);
                    
    document.getElementById('mbrFee').value memFee.toFixed(2); 
                }
                
    document.getElementById('GrandTotal').innerHTML '$'+grandTotal.toFixed(2); 
            }); 
        
    </script> 
    (I've omitted extraneous code but I think everything that's pertinent is included here)

    The math works, if a quantity value is entered that is greater than the "limit" the the total value is calculated using the "limit" instead of the input value.

    However I have two issues:
    1. The value in the input field stays at the "overlimit" value. Is there a way to replace the integer in the input field with the "limit" value?
    2. All of the products are using the same value for "limit" inspite of the fact that $customer_max is only greater than zero for one.

    Any suggestions greatly appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    501
    Rep Power
    49
    Originally Posted by SeanF
    PHP :
    $max_note = "<br/> &nbsp &nbsp &nbsp &nbsp (Max: <span id=\"cust_max\">$customer_max</span>)";

    javascript:
    var limit = Number(document.getElementById('customer_max').innerHTML);
    .
    Ah... found the problem with #2... I needed to change the lines shown above to:

    Code:
    PHP:
    $max_note = "<br/> &nbsp &nbsp &nbsp &nbsp (Max: <span id=\"cust_max[$i]\">$customer_max</span>)";
    
    and
    
    javascript:
    var limit = Number(document.getElementById('customer_max['+unitSubId+']').innerHTML);
    But I am still not sure how I would replace the types quantity value with the "limit" value. Any help on that would be great.

IMN logo majestic logo threadwatch logo seochat tools logo