2009/09/05

PHP/HTML Input Name Limit

Filed under: osCommerce Coding — Tags: , , — eCartz @ 15:25  Share/Bookmark  Delicious  StumbleUpon  WordPress  Twitter  LinkedIn

Worked through a weird bug the other day.  A gaming computer store has products with lots of attributes — as many as fifty.  Since osCommerce stores the attributes with the product ID, the result is to make the id array that holds the attributes on the shopping cart page have exceptionally large input names.  If the name gets too large (I didn’t test it exactly, but more than two hundred characters was all right and three hundred some broke it), it gets silently dropped. 

The behavior that this caused in osCommerce was to make the attributes disappear any time that the cart was updated (item removed or a quantity changed).  The problem being that it was using the posted variables to determine the attributes.  I’m not sure why it does this.  It should have all the required information in the $cart variable stored in the session.  Perhaps it is trying to mirror the product_info page behavior. 

A surprisingly small change fixed this.  I simply replaced the products ID string with the numerical placement in the list.  I.e., for the first item, it’s ID became 0.  The second item was 1 and so on.  I changed it once in the shopping cart page and once in includes/application_top.php.  This decreases the robustness somewhat (we are now relying on order being preserved through the POST), but it worked in my testing. 

The includes/application_top.php change (only 354 changed; the others are just for context and to make the code highlighter offer a scroll bar): 

 PHP |   copy code |? 
353
                                  } else {
354
                                    $attributes = ($HTTP_POST_VARS['id'][$i]) ? $HTTP_POST_VARS['id'][$i] : '';
355
                                  }
356
                                  $cart->add_cart($HTTP_POST_VARS['products_id'][$i], $HTTP_POST_VARS['cart_quantity'][$i], $attributes, false);

 

The shopping_cart.php change (only 86 changed): 

 PHP |   copy code |? 
81
    $products = $cart->get_products();
82
    for ($i=0, $n=sizeof($products); $i<$n; $i++) {
83
// Push all attributes information in an array
84
      if (isset($products[$i]['attributes']) && is_array($products[$i]['attributes'])) {
85
        while (list($option, $value) = each($products[$i]['attributes'])) {
86
          echo tep_draw_hidden_field('id[' . $i . '][' . $option . ']', $value);

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You can log in to post a comment, or just fill out your name and email.

Powered by WordPress