从AJAX调用数据

时间:2017-05-19 15:13:06

标签: javascript php jquery ajax

我有一个脚本可以将画布PNG保存到目录中,我需要将URL保存在服务器上。

当我提交时,图像会保存,但服务器仍为空。我试图返回错误消息,但我什么也没收到。

JAVASCRIPT:



function doodleSave() {
	
	
	var drawing			=	document.getElementById("doodle-canvas");
	var drawingString	= 	drawing.toDataURL("image/png");
	var postData		=	"canvasData="+drawingString;
	
	var ajax 			=	new XMLHttpRequest();
	
	ajax.open("POST", 'http://www.website.com/php/doodleSave.php', true);
	ajax.onreadystatechange= function() {
		if (ajax.readyState === 4) //If it ran smoothly

		
				{$("#doodle-submit-button").html("...");}
		
		
	};
	ajax.send(postData);
	
	ajax.success(function(data) {
		
		{$("#doodle-submit-button").html(""+data+"");}
		
	});
	
}




PHP:



<?php
session_start();

if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) {

			$rawImage		=	$GLOBALS['HTTP_RAW_POST_DATA'];
			$removeHeaders	=	substr($rawImage, strpos($rawImage, ",")+1);
			$url			=	md5(uniqid(rand(), true));
			$decode			=	base64_decode($removeHeaders);
			$fopen			=	fopen('../images/external/doodles/'.$url.'.png', 'wb');
			fwrite($fopen, $decode);
			fclose($fopen);


			//ADD POST TO DATABASE WITH USER'S ID

			/* AUTOMATED VARIABLES */
			$unique_user_id	= $_SESSION['unique_user_id'];
			$unique_post_id	= md5(uniqid(rand(), true));
			$timestamp		= time();
			$nature			= "doodle";
			$imageUrl		= 'images/external/doodles/'.$url;


			try
			{

				$stmt = $pdo->prepare("INSERT INTO `(table name)` (unique_user_id, unique_post_id, nature, image_url, timestamp) VALUES(:unique_user_id, :unique_post_id, :nature, :image_url, :timestamp)");
				$stmt->bindParam(":unique_user_id",$profile_unique_user_id);
				$stmt->bindParam(":unique_post_id",$unique_post_id);
				$stmt->bindParam(":nature",$nature);
				$stmt->bindParam(":image_url",$imageUrl);
				$stmt->bindParam(":timestamp",$timestamp);

						if($stmt->execute())
						{
							echo "uploaded";
						}
						else
						{
							echo "Could not upload";
						}

				}
				catch(PDOException $e){
					echo $e->getMessage();
				}
	}

?>
&#13;
&#13;
&#13;

0 个答案:

没有答案