Dragen Posted November 20, 2007 Share Posted November 20, 2007 Hi, I've got a php script which takes an x and y coordinate and gets the pixel colour from an image with those coordinates: <?php class getcolor{ function getcolor($x, $y){ $image = @imagecreatefrompng('palette.png'); $x = (integer) $x; $y = (integer) $y; if($image){ $color = ImageColorAt($image, $x, $y); $r = ($color >> 16) & 0xFF; $g = ($color >> & 0xFF; $b = $color & 0xFF; $this->color = $this->gethex($r, $g, $b); }else{ return false; } } function gethex($r, $g, $b){ return strtolower(sprintf('#%02X%02X%02X', $r, $g, $b)); } } $getcolor = new getcolor(50, 86); // the colour is stored as hex in the variable: $getcolor->color; ?> Now what I want to do is use ajax to create an onclick event which calls up the getcolor function with the specified x and y, then retrieves the given value. How can I send and recieve from a php file with ajax? I've seen something in javascript about the call metho, but am unsure if this is the right direction. Thanks Quote Link to comment Share on other sites More sharing options...
Dragen Posted November 21, 2007 Author Share Posted November 21, 2007 nevermind. I've figured it out.. javascript syntax is really driving me nuts though here's my solution: getcolor.php: <?php class getcolor{ function getcolor($x, $y){ $this->color = 'n/a'; $image = @imagecreatefrompng('palette.png'); $x = (integer) $x; $y = (integer) $y; if($image){ $color = ImageColorAt($image, $x, $y); $r = ($color >> 16) & 0xFF; $g = ($color >> & 0xFF; $b = $color & 0xFF; $this->color = $this->gethex($r, $g, $b); } } function gethex($r, $g, $b){ return strtolower(sprintf('#%02X%02X%02X', $r, $g, $b)); } } if((isset($_GET['x']) && isset($_GET['y'])) && (ctype_digit($_GET['x']) && ctype_digit($_GET['y']))){ $getcolor = new getcolor($_GET['x'], $_GET['y']); }else{ $getcolor->color = 'n/a'; } echo $getcolor->color; ?> as you can see, at the end I echo out the variable. Then I have an onclick event which calls the javascript getcolor function: <a href="#" onclick="javascrip:javascript:getcolor(X, Y);">click here</a> //x and y are my x and y coordinates The javascript looks like this: var xmlhttp function getcolor(x, y){ xmlhttp=null // code for Mozilla, etc. if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest() } // code for IE else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") } if (xmlhttp!=null) { xmlhttp.onreadystatechange=state_Change xmlhttp.open("GET","/admin/includes/getcolor.php?x="+x+"&y="+y,true) xmlhttp.send(null) } else { alert("Your browser does not support XMLHTTP.") } } function state_Change() { // if xmlhttp shows "loaded" if (xmlhttp.readyState==4) { if (xmlhttp.status==200) { var response = xmlhttp.responseText; alert('response: '+response); } else { alert("Problem retrieving XML data") } } } Which correctly gives me anything that is output from my php file. All I need to do now is get it to put the value into a text input, which should be fairly easy. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.