how to save canvas image to server?

I have created drawing app as like below:

http://www.samrejoiceson.com/Portfolio/HTML5/drawapp/

Please try a link in Chrome browser. Currently this works only chrome and safari.
I need to save the canvas image to server. My html code is below:

<!DOCTYPE html>
<html>
<head>
<title>Drawing App</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
<link rel="stylesheet" type="text/css" href="../js/style.css"/>
<script type="text/javascript">
function saveViaAJAX()
{
var testCanvas = document.getElementById("canvas");
var canvasData = testCanvas.toDataURL("image/png");
var postData = "canvasData="+canvasData;
//var postData = canvasData;
var debugConsole= document.getElementById("debugConsole");
debugConsole.value=canvasData;

var textarea_value= document.getElementById("answer").value;


if((textarea_value=='')||(textarea_value=='Your Answer'))
{
	alert('Enter description of the image');
	return false;
}
else
{
	alert('Your image and description updated successfully');
}
//alert("canvasData ="+canvasData );
var ajax = new XMLHttpRequest();
ajax.open("POST",'testSave.php?textarea_value='+textarea_value,true);
ajax.setRequestHeader('Content-Type', 'canvas/upload');
//ajax.setRequestHeader('Content-TypeLength', postData.length);


ajax.onreadystatechange=function()
{
if (ajax.readyState == 4)
{
// Write out the filename.
//document.getElementById("debugFilenameConsole").innerHTML="Saved as : <a target='_blank' href='http://www.samrejoiceson.com/Portfolio/HTML5/drawapp/draw_Resources/file/newtest.html'>"+"view image"+"</a><br/>Reload this page to generate new image or click the filename to open the image file.";
}
}

ajax.send(postData);
}
</script>
</head>
<body>
<canvas id="canvas" width="900" height="600"></canvas>
<textarea id="color">colorBlack</textarea>&#x00A0;<textarea id="size">small</textarea>
<textarea id="debugConsole" rows="10" cols="300">Data</textarea>
<textarea id="answer" rows="1" cols="46" onBlur="if (this.value == ''){this.value = 'Your Answer'; }" onFocus="if (this.value == 'Your Answer') {this.value = '';}">Your Answer</textarea>&nbsp;<img class="none" src="../js/images/submitbutton.jpg" onclick="saveViaAJAX();"/>
<div id="debugFilenameConsole">John</div></body></html>

Open in new window

My php code is below:
<?php
if (isset($_POST['canvasData'])) {
// Get the data
$imageData=$_POST['canvasData'];

// Remove the headers (data:,) part.
// A real application should use them according to needs such as to check image type
$filteredData=substr($imageData, strpos($imageData, ",")+1);

// Need to decode before saving since the data we received is already base64 encoded
$unencodedData=base64_decode($filteredData);

//echo "unencodedData".$unencodedData;

// Save file. This example uses a hard coded filename for testing,
// but a real application can specify filename in POST variable
$fp = fopen( ‘test.png’, ‘wb’ );
fwrite( $fp, $unencodedData);
fclose( $fp );
}
?>

Open in new window

But it not saved canvas image to my server. Please advice how to do this?
JohnLourduAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Use this function to see what the $_POST data looks like.  Then show us, too.
http://php.net/manual/en/function.var-dump.php
0
Ray PaseurCommented:
And just a thought, but one that will save you from a lot of problems.  Use jQuery so you get cross-browser JavaScript support.  The "Hello World" jQuery+AJAX exercise is illustrated in this article:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnLourduAuthor Commented:
As per below post, I have found above code has performance issue:
http://mitgux.com/send-canvas-to-server-as-file-using-ajax
So I used the second method of the post. Now it works fine for me.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.