Jump to content

Javascript alerts and header function


gonk

Recommended Posts

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 after
Example 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 one

if ($_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 alerts


echo "<script language='JavaScript'>" ;
echo "document.NewService.preview.src='../images/$imgname';";
echo "</script>";
?>

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.