0

Following is my code :

<div id="accept" style="background-color:#83CC3F;"></div>
<script type="text/javascript" >
function accept1() {
return document.getElementById('accept').style.backgroundColor;
}
 </script>
<?php
$js=<<<EOF
<script type="text/javascript" >
document.write(accept1());
 </script>
EOF;
echo $js;
if($js == "rgb(131, 204, 63)") //rgb(131, 204, 63) rgb equivalent of hex code #83CC3F
{
echo "yes"; 
}
?>

Above code doesn't return "yes" whereas $js=rgb(131, 204, 63) , Please solve my problem

user47288
  • 40
  • 6

2 Answers2

0

you can't take var like this in php..

$js=<<<EOF
<script type="text/javascript" >
document.write(accept1());
 </script>

because php is server side language .. and javascript is client side.

For this you have to use ajax ,get and post methods

UPDATE1:

It's always always important to remember that Javascript executes on the clients browser, & PHP executes on the server. So $js has no idea what

<script type="text/javascript" >
document.write(accept1());
 </script>

will be, because that won't execute until it reaches the viewers browser. & just as well, once the viewers browser gets it, PHP is done executing, so it can't hand the output off.

UPDATE2: If you do the following it will echo yes.

if($js == '<script type="text/javascript" >document.write(accept1());</script>'); //rgb(131, 204, 63) rgb equivalent of hex code #83CC3F
{
echo "yes"; 
}

This is because PHP variable $js still has <script type="text/javascript" >document.write(accept1());</script> value set.

Dushyant Joshi
  • 3,672
  • 3
  • 28
  • 52
0

Your must check this codes in js, but PHP not render the content and not get color from html element.

For checking RGB by eq to HEX, you can convert each part RGB to DEC and concatenation this string, and after check eq.

For example RGB(10, 20, 30):

DECTOHEX(10) . DECTOHEX(20) . DECTOHEX(30) == '#0A141E'

ZhukV
  • 2,892
  • 6
  • 25
  • 36