gonk Posted January 2, 2007 Share Posted January 2, 2007 I'm new to php and trying to get php html and javascript acting in a coordinated fashion. Basically I want to run a php script and then return to a html file sending some javascript alerts and or setting html control values on the way back and on arrival to the html page. Using the php include() statement lets me do this most of the time but using the php header function doesn't. I 'like' the header function cause it leaves the user in the actual html page rather than leaving them in the php file that has the html page included. Does anyone know if theres something I've left out of the header specification or an alternative to the header function (other than include) that will allow me to do what I'm afterExample of a php prog that saves images to the server.<?php$ID=2; $imgname='img_'.$ID ;//Check if the image type from the selected file is gif or jpeg. //Copy it to the server saving it as a name unique to the user but replacing their naming convention with a consistent oneif ($_FILES['in_pic']['type'] == "image/jpeg" || $_FILES['in_pic']['type'] == "image/gif"){ if ($_FILES['in_pic']['type'] == "image/jpeg") { $imgname=$imgname.".jpg"; } else {$imgname=$imgname.".gif"; } copy ($_FILES['in_pic']['tmp_name'], "../images/".$imgname) or die ("Could not save image."); } else { // echo "Could Not Copy, Wrong Filetype (".$_FILES['in_pic']['size'].")<br>"; echo "<script language='JavaScript'> alert('Save failed. Wrong filetype or size.'); </script>"; } //header ("Location: SCSServicesAdd.html"); DOESN't set the src or show alerts include "SCSServicesAdd.html"; //DOES allow setting the src & showing alertsecho "<script language='JavaScript'>" ;echo "document.NewService.preview.src='../images/$imgname';";echo "</script>";?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 2, 2007 Share Posted January 2, 2007 You can not do what you want to do without using AJAX, since PHP runs on the server and Javascript runs on the client. By the time Javascript executes, PHP has already executed and sent it's output to the browser.Ken Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 2, 2007 Share Posted January 2, 2007 I've read your post a few times and it is very confusing. I think you may have some misunderstandings that are complicating the matter. Your statements about going from a PHP file to the HTML page and vice vers make no sense. The PHP page creates the HTML page - you don't go from one to the other.The problem in your case is that ALL php header functions must be run before you write any content to the HTML page. Since you have written the javascript to the page, you cannot use the header function.I'm sure there is a much better method to handle this issue, but since you asked: One option would be to include a javascript command to open the redirect page right after the alert. As soon as the user clicks OK they will be redirected. 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.