Disable input fields based on selection of mysql database item

I have an order form for lots of apparel; it gets populated using cascading selects from a database. The next section of the form is a series of input fields to enter quantities for each size. There are 13 sizes ranging from YXS-4XL. I want to give my database items an attribute that will disable the input fields for sizes that are not available for that particular garment.

What is the best way to do this? At the moment all I can think of is to insert a column in the database for each size and then call for it to return true or false on the form. But that seems like it can't be the best way to go about doing this. Keep in mind I have lots of database items to do this for.


To shorten things you could use a single flag-column (integer), indicating bit by bit which sizes are available for each article. The flag values are composed of these values which correspond to the available sizes:

flag   size  2^ (bit no.)
1      XS    0
2      S     1
4      M     2
8      L     3
16     XL    4

and so on. To have mutliples sizes available just enter the sum of the two size flags:

6      S,M

The individual sizes can be easily checked for by using a bitwise AND operator (&) which is available in MySQL, PHP and JavaScript. A standard int column (4 bytes) already allows you to keep track of up to 32 different sizes and all their possible combinations.

In an SQL statement, when you want to make sure a size M is available you simply use the WHERE clause

WHERE flag&4 > 0

Or in a JavaScript section you can easily determine which checkboxes to activate by using something like active = xl_flg & flag > 0 where flag is the variable containing the value from your MySQL column (possibly received through AJAX) and xl_flag is an attribute of your input field (or from the surrounding <div>). You can then act on the value of active (true or false) to hide() or show() the input element.

If you want to set or unset the availability of an item in your back office simply do this (PHP):

// working with constants makes your life easier ... ;-)
define("XS", 0);
define("S",  1);
define("M",  2);
define("L",  4);
define("XL", 8);

// typically the following information is provided by an AJAX post 
// for example by adding the values of checked checkboxes ...
$sizes= S + M + XL; // = 11 

-- set (MySQL query string in PHP):
$sql = "UPDATE ... SET flag = flag & $sizes WHERE ... ";

-- unset 
$sql = "UPDATE ... SET flag = flag & ~$sizes WHERE ... ";

~ inverts all the bits in a number (acts like a logical NOT). The combination of & and ~ switches off the bits set in the second operand.

You can disable fields like this using jquery

    $('selector').attr('disabled', 'disabled');

To enable again


use AJAX calls when onchange garment input field.

{'garment' : 'garment_name_or_id'} 

then PHP checks about available sizes in database and return.

echo json_encode($mysql_result);

then disable input fields using prop().

$("input").prop('disabled', true);

Need Your Help

It is not recognising a php variable as nothing appears on echo

php mysqli bind

in my code below it is not able to recognise the variable $userid, this variable determines the id of the teacher logged in but in the mysqli code it is not able to determine the $userid when I ech...

How does one parse simple inline markup (i.e. *bold*), in Python?

python parsing wikitext creole

How does one implement a parser (in Python) for a subset of wikitext that modifies text, namely:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.