954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Passing a color value from color picker to a PHP Form Variable

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

arvindikchari
Light Poster
41 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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

katarey
Junior Poster
167 posts since Jul 2005
Reputation Points: 39
Solved Threads: 23
 

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

arvindikchari
Light Poster
41 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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;
/*--><?*/
?>
essential
Posting Shark
974 posts since Aug 2008
Reputation Points: 114
Solved Threads: 138
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You