Zaper Gallis Posted December 31, 2010 Share Posted December 31, 2010 Not sure whats going on here. Basically I'm using php to generate a table full of colors with "Onclick" commands to trigger a java-script and pass a hex color code threw it as a variable. Everything in the code works, until you try to pass the letters threw, in which case i get several strange errors in my Firefox error counsel, depending on were you click. needless to say, the script doesn't work, and i have no idea what could be wrong. Here's the script: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Color Picker</title> <script type="text/javascript"> function colorselect(color) { document.colorpicker.hexselect.value= color; } </script> </head> <body> <form name="colorpicker" id="colorpicker" action=""> <?php $red= "00"; $green= "00"; $blue= "00"; $code= "000000"; ?> <table style="background-color: #000000;"> <tr> <th colspan="18" style="background-color: #ffffff;"> Color Selector </th> </tr><tr> <?php while ($blue != stop) { ?> <td><div style="height:15px; width: 15px; background-color: #<?php echo $code; ?>" onclick="colorselect(<?php echo $code; ?>)"></div></td> <?php if ($code== FFFFFF) {$stop= stop;} if ($red== "00") {$red="33";} else if ($red== "33") {$red= "66";} else if ($red== "66") {$red= "99";} else if ($red== "99") {$red= "cc";} else if ($red== "cc") {$red= "ff";} else if ($red== "ff") { $red= "00"; if ($green== "00") {$green= "33";} else if ($green== "33") {$green= "66";} else if ($green== "66") {?></tr><tr><?php $green= "99";} else if ($green== "99") {$green= "cc";} else if ($green== "cc") {$green= "ff";} else if ($green== "ff") { ?></tr><tr><?php $green= "00"; if ($blue== "00") {$blue= "33";} else if ($blue== "33") {$blue= "66";} else if ($blue== "66") {$blue= "99";} else if ($blue== "99") {$blue= "cc";} else if ($blue== "cc") {$blue= "ff";} else if ($blue== "ff") {$blue= stop;} } } $code = $red . $green . $blue; } ?> </tr><tr> <td colspan="18"> <input type="text" name="hexselect" id="hexselect" /> </td> <tr> </table> </form> </body> </html> and here's the sort of working web page it's on: http://www.webmasterseverything.com/colorpicker.phpFar as i know the PHP part works fine, as it echo's all the data it's supposed to, so I'm pretty sure it's in the java script. You guys have any ideas?On a sub note, i notice hex codes with 0s at the start get removed by the java script, is it possible to make the script keep the 0s? Link to comment Share on other sites More sharing options...
Fmdpa Posted December 31, 2010 Share Posted December 31, 2010 It's because you are passing in the values as an integer, and some color values include letters. Enclose the function argument in quotes, and it should fix your problem.colorselect("ff0000") Link to comment Share on other sites More sharing options...
Zaper Gallis Posted December 31, 2010 Author Share Posted December 31, 2010 I've tried that but it then gives me an error saying "invalid charactor" or something like that. Any clue why? Link to comment Share on other sites More sharing options...
jeffman Posted December 31, 2010 Share Posted December 31, 2010 We'd have to see it printed out the other way to know why it didn't work as expected..To clarify what's happening now. Sometimes it works. More often it doesn't. When it works, it is when the value being passed to colorselect is ALL digits. JavaScript is implicitly casting the value to a string, and the string is inserted in your text box. When it doesn't work, it is because (1) the value contains letters, in which case JavaScript doesn't understand it, or (2) it begins with letters, in which case JavaScript thinks it is an undefined variable.This change should work:onclick="colorselect('<?php echo $code; ?>')"Notice the additional single quotes surrounding the PHP tags. Link to comment Share on other sites More sharing options...
Zaper Gallis Posted January 1, 2011 Author Share Posted January 1, 2011 Ok, the singe quotes worked. Thanks for the help guys!. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.