Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Redirect after successful data post in Ajax

ajax php

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 PuddingEatsPanda

PuddingEatsPanda

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 57 posts

Posted 16 February 2016 - 12:38 AM

Hi! I followed a tutorial on how to save a photograph snapped via webcam using Ajax. I can save the data but I cannot find a way to redirect when successful.

This is the code for saving the photograph

function save(){
	//saving
	var myDrawing = document.getElementById("canvas"), 
	drawingString = myDrawing.toDataURL("image/png"),
	postData = "canvasData"+drawingString,
	ajax = new XMLHttpRequest();
	ajax.open("POST", 'saveImage.php', true);
	ajax.setRequestHeader('Content-Type', 'canvas/upload');
	ajax.onreadystatechange = function()
	{
		if (ajax.readyState == 4 && ajax.status == 200){
			alert("Image saved!");
		}
	}
	if(ajax.send(postData)){ //hoping that if successful it will redirect
		window.location = "start.php";
	}
}

This is the PHP file it uses for saving (I think it is called parsing? Please correct me if I am wrong) - SaveImage.php

<?php
session_start();
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
	$rawImage=$GLOBALS['HTTP_RAW_POST_DATA'];
	$removeHeaders=substr($rawImage, strpos($rawImage, ",")+1);
	$decode=base64_decode($removeHeaders);
	$fopen = fopen('uploads/' . $_SESSION['idnum'] . '.png', 'wb');
	fwrite($fopen, $decode);
	fclose($fopen);
	header('Location: start.php'); //Tried to redirect using this but it does not work
}

?>

Thanks!


Edited by PuddingEatsPanda, 16 February 2016 - 12:38 AM.

Want to learn MOAR