Hello,

I have a script which uses a colorpicker in which the user can select a color, and in the preview section the color of one div is changed to this color value. This is done by a javascript function of the color picker. called setcolor. The code for this function is given below:

function setColor(color) {
	var inp = document.getElementById("pageSurround");
	inp.style.backgroundColor =  color;
}

Now I want that the value 'color'(used above to set bg color for the element 'pageSurround') is passed to a form variable named 'in_backgroundColor' (the color picker is being invoked in a PHP Script) so that this value can be stored in the database. How should this be coded.

Regards
Arvind

HI Arvind,

well as I understand you want to set the input (Form element) "in_backgroundColor" value as same as div "pageSurround's" background color or javascript variable "color", if I am right then you can add these Lines in setColor function

var inputName = document.getElementById("in_backgroundColor");
inputName.value = color;

or

document.formName.in_backgroundColor.value = color;

now function setColor shoud look like

function setColor(color) {
      var inp = document.getElementById("pageSurround");
      inp.style.backgroundColor = color;

      var inputName = document.getElementById("in_backgroundColor");
      inputName.value = color;
}

or

function setColor(color) {
      var inp = document.getElementById("pageSurround");
      inp.style.backgroundColor = color;
      document.formName.in_backgroundColor.value = color;
}

Hope this will help you

Rahul Dev Katarey

commented: concise answer to the problem +1

Hello Rahul,

I looked at the documentation for the color picker and it has an example for passing the color value:

<script>
 
    function init4() {
    //get selected color
    var z=new dhtmlXColorPicker("null, true");
    z.init();
    var c= z.getSelectedColor();
    alert('#'+c[0]);
    }

</script>

How do I pass the color value to a variable in a PHP Form

Regards
Arvind

Hi there! You can provide the variable(s) you need with the use of a hidden field's.
Let's assume that you have this hidden field in your document.

<input type ="hidden" name="colour" id="colour" value="" />

now with your script on simply change line 8, with this

document.getElementById('colour').value = '#' + c[0];

then let's embed its value to see the result in your document.

<p><?php /*><!--*/ 
 if(isset($_REQUEST['colour'])):
echo $_REQUEST['colour'];
else:
echo 'Failed to set variable(s)!';
endif;
/*--><?*/
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.