Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

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


    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(){
	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

	$removeHeaders=substr($rawImage, strpos($rawImage, ",")+1);
	$fopen = fopen('uploads/' . $_SESSION['idnum'] . '.png', 'wb');
	fwrite($fopen, $decode);
	header('Location: start.php'); //Tried to redirect using this but it does not work



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

Want to learn MOAR

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download