Jump to content

JScolor to PHP Variable


cjenkins08
Go to solution Solved by Jacques1,

Recommended Posts

I am working with this color picker code from jscolor.com and I am trying to use this particular example

<script type="text/javascript" src="jscolor/jscolor.js"></script>
<script type="text/javascript">
function updateInfo(color) {
    document.getElementById('info-r').value = color.rgb[0];
    document.getElementById('info-g').value = color.rgb[1];
    document.getElementById('info-b').value = color.rgb[2];
}
</script>

Color: <input class="color {onImmediateChange:'updateInfo(this);'}" value="66ff00">
R: <input id="info-r" size="2" />,
G: <input id="info-g" size="2" />,
B: <input id="info-b" size="2" />

After a submit button is clicked I need to assign variables to each of the RGB in PHP, but I'm not quite sure how to take those javascript values over to PHP.

Any help is appreciated.

Link to comment
Share on other sites

I am trying as you said and pulling them with $post using the following code in my php file

$colorr = $_POST['color'][0];
$colorg = $_POST['color'][1];
$colorb = $_POST['color'][2];

The form is the same as original post except I added the name property to the input field

<form action="100-text-no-color.php" name="genForm" method="post">

Color: <input name="color" class="color{onImmediateChange:'updateInfo(this);'}" value="66ff00">
R: <input id="info-r" size="2" />,
G: <input id="info-g" size="2" />,
B: <input id="info-b" size="2" />

<input type="submit" value="Generate Grid" style="height: 40px; width: 180px; font-size: 17px;" />

</form>

When I go to display the variables, I am getting the first 3 digits of the HEX code as opposed to the RGB values. What am I doing wrong?

Thanks

Link to comment
Share on other sites

I think you're just confused because of all the fancy JavaScript.

 

You have four input elements, right? The first one holds the hexadecimal value of the color. That's also the one you've called “color”, so the “color” parameter will contain a string with 6 hexedecimal digits. Not exactly surprising.

 

You want to use the other input fields? Well, then give those a name. Call them “r”, “g”, “b”. Or call them “color[0]”, “color[1]” and “color[2]” to make the “color” parameter an array.

Link to comment
Share on other sites

Oh yeah, I'm confused alright. I'm still new, but I'm trying.

Your post made sense to me and I made some progess, but have one more stumbling block.

I have this html

Color: <input name="color" class="color{onImmediateChange:'updateInfo(this);'}" value="66ff00">
R: <input id="info-r" name="colorr" size="2" />,
G: <input id="info-g" name="colorg" size="2" />,
B: <input id="info-b" name="colorb" size="2" />

and this in my php file

$colorr = $_POST["colorr"];
$colorg = $_POST["colorg"];
$colorb = $_POST["colorb"];

So this is now working to pull the value of the RGB fields, the problem is when jscolor sets these values it seems unless it is a 0 or 1 they put a decimal in front of the number and some are longer than 3 digits.

Example

R 0.08999999999999997

G 1

B 0.286

 

So when I go to display these colors they are not recognized.

How can I strip the decimal and the number in front of the decimal and also strip any digits past the decimal that are more than 3 places out :facepalm:

 

Thanks for the help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.