Anyone know of a way to put a color picker into a form, to allow a user to choose a color from either their standard system color picker or (more likely) a standard websafe color picker? The result would probably be the hex value of the chosen color, which could then be stuffed into a MySQL table.

Perhaps this sort of thing has been done in Javascript, rather than PHP. I'm just fishing at the moment - don't really know where to start. I want to allow a user to customize the presentation of their data - choose font color, size, face, etc. The size and face are easy, but short of creating a popup with every color they might possibly want to use (and no swatches - just names) I don't know how to do the color part.

    That would be a Javascript job. It wouldn't be too difficult to have a loop that builds a table of websafe colours. Maybe give individual cells a background of that colour and put a link inside the cell (use the hex value for the cell text I suppose).

    The link would be to the page that puts the colour in the database, with a querystring that specifies the hex value.

    action.php?colour=FF9900

    say, and in action.php stuff the value of $_GET['colour'] into the database (after of course first checking to see if it's a proper colour hex value!)

    If you're interested, the loop would go from 0 to 215 inclusive. If i is the loop counter, the corresponding hex code is

    var d2h='0369CF'; // Best outside the loop!
    b = i%6;
    g = ((i-b)/6)%6;
    r = ((i-g)/6)%6;
    b = substr(d2h,b,1);
    g = substr(d2h,g,1);
    r  = substr(d2h,r,1);
    hex = '' + b + b + g + g + r + r;
    

    (assuming that's proper Javascript, I forget for the instant if its substr() function looks exactly like that).

    Note that I haven't bothered with the # - might as well leave that for when it's actually used. You'd only have to escape it as %23 to put it in the querystring anyway.

      MS has a solution for MSIE. If that's OK for you, look for it on MSDN searching for the word "ChooseColorDlg"

        My code could be written in PHP, of course; but if you do it in Javascript you can have a "sample' somewhere on the page that gets updated in real time as various choices are made. Instead of a link, the table cells contain something with an onClick() handler that (a) updates the sample, and (b) updates a hidden field in a form. Then the user can fiddle around, see how things look, and then when satisfied hit a button that submits the form.

          Write a Reply...