Jump to content

Need A Pop-up Executed From Php


creacon

Recommended Posts

I have a page that after capturing data from a form (via $_POST), an email is sent, and I need to be able to display a success or failure alert box type message. Success or failure are not known until the php script which, executes as a result of an if isset..., attempts to send the email. At that point I need for the appropriate message to pop up, and remain visible until the user clicks on an OK or Close button. I looked through jQuery, but most everything I found there uses javascript functions, and I can't seem to make javascript work, because I haven't found a way to execute a javascript function from a php script. Some of the jQuery plug-ins were beyond my ken to understand how to use them, let alone whether they'll work the way I need them to. Here's my php code that sends the email:

		$to		 = "creacontech@windstream.net";		$from	 = "rob@creacontech.com";		$headers = "From: $from";		$subject = "WOTC Questionnaire Report Request";		$message = $cust." has executed a WOTC Questionnaire for: \n \n".					" ".$arrappl[fullname].", SSN: ".$arrappl[ssn].", DOB: ".$arrappl[dob].					", ".$arrappl[race].", ".$######."\n Address: ".$address.", ".					$arrappl[city].", ".$arrappl[state].", ".$arrappl[zip].", Phone: ".					$arrappl[phone]."\n \n Recruited By: ".$arrappl[recname].", Title: "					.$arrappl[rectitl].", Phone: ".$arrappl[recphn].", Extn: ".$arrappl[recphnext].					"\n Address: ".$rcraddr.", ".$arrappl[reccity].", ".					$arrappl[recstate].", ".$arrappl[reczip]."\n Email: ".$arrappl[recemail].					"\n \n and has requested the following reports: \n \n";		for ($i=0; $i<=$numreps; $i++)		{			$message = $message.$arrpsel[$i]."; ";		}		if (mail($to,$subject,$message,$headers))		{			printf("The following email was sent:<br /><br />    To:  %s<br /><br />Subj:  %s<br />				   <br />Your WOTC Questionnaire for:<br /><br />  Name:  %s",				   $to,$subject,$arrappl[fullname]);		}		else		{			echo "<br />An ERROR was encountered and the email WAS NOT SENT<br />			Contact your webmaster for assistance.<br /><br />";		}		

I think there was a little censoring action up there; the $###### in the message should be $###### - it's the gender of the applicant, Male or Female.Naturally those "printf's" are for debugging purposes, since what they display isn't visible on the finished form. I tried using a hidden field and then un-hiding it following the above code. That didn't work either, because the when the field is unhidden, it just blinks for an instant, and then the page refreshes itself (even if I don't have it go to the next page). I'm incredulous that php isn't capable of displaying an alert type box, or if it can, I haven't found out how anywhere.Can someone please help me with this? I can't believe that this is so difficult to do.

Link to comment
Share on other sites

Blah. What jsg said.

Link to comment
Share on other sites

You can use PHP to print the Javascript code to show the alert.
		else		{			echo '<script type="text/javascript">alert("An ERROR was encountered and the email WAS NOT SENT.  Contact your webmaster for assistance.");</script>';		}

Thanks a million for the input. That worked well; didn't know about being able to do that. I wish these tutorials went a little deeper into this sort of thing.
Link to comment
Share on other sites

You can use PHP to print the Javascript code to show the alert.
			echo '<script type="text/javascript">alert("A WOTC Questionnaire email WAS SENT to" + '\n' + "Information On Demand." );</script>';

Which is pretty much the way it's shown in the W3Schools reference example:

script type="text/javascript">function disp_alert(){alert("Hello again! This is how we" + '\n' + "add line breaks to an alert box!");}</script></head><body><input type="button" onclick="disp_alert()" value="Display alert box" />

When I tried that, I got the following error:

Parse error: syntax error, unexpected T_NS_SEPARATOR, expecting ',' or ';' in C:\xampp\htdocs\IOD\WOTCPg3.php on line 156

Isn't it possible to add line breaks when using the javascript in the php echo? Or is there another way to do that?

Link to comment
Share on other sites

It's just a quote issue, since you're printing the Javascript from PHP the quotes are getting messed up. You can use double quotes for the newline instead, or you actually don't even need to close the string for the newline.echo '<script type="text/javascript">alert("A WOTC Questionnaire email WAS SENT to\nInformation On Demand." );</script>';

Link to comment
Share on other sites

It's just a quote issue, since you're printing the Javascript from PHP the quotes are getting messed up. You can use double quotes for the newline instead, or you actually don't even need to close the string for the newline.echo '<script type="text/javascript">alert("A WOTC Questionnaire email WAS SENT to\nInformation On Demand." );</script>';
Thanks, that worked great. I really appreciate your help. I have to wonder if I'll ever learn all these things. In my 50 years of programming, I must have learned 30 or more programming languages, but none has confounded me as much as this. I don't know if it's because this is so much more complicated, or if it's just my growing senility (I'm 77). I really have to admire and respect you guys who understand all this.
Link to comment
Share on other sites

Here's a slightly different twist on the same problem. I have another page that is only for users with "Administrator" access, so I added a php script at the beginning to check for that. Here's my code:

	session_start();		//	Fetch required session variables	$userlevel = $_SESSION['MM_UserGroup'];	$userlogin = $_SESSION['MM_Signin'];		//	Check to see if the current user	//	is authorized to access this page	if ($userlevel != "Administrator")	{	echo '<script type="text/javascript">alert("YOU ARE NOT AUTHORIZED TO ACCESS THIS PAGE.")		;</script>';		echo '<META http-equiv="refresh" content="0;URL=WOTCSignin.php">';	//		header("Location: " . $userlogin ); 	}

It sort of works, but; 1) when I OK the alert box, it pops up for a second time, then when I OK that it goes to the signin page, and 2) I had to use the "META" because the "header()" gives the following error, then displays the page anyway:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\IOD\WOTCAdminMenuTest.php:13) in C:\xampp\htdocs\IOD\WOTCAdminMenuTest.php on line 17

Am I missing something?

Link to comment
Share on other sites

When using header("Location: area.html"); you cannot have any text displayed on the page before header("Location: area.html");Example - this will NOT work - it would display the same error:

<?echo "Hooray!";header("Location: http://www.google.com");?>

Hope it helps!

Link to comment
Share on other sites

Yeah, it's better to use a meta tag for this since you want to display content on the page (the Javascript code). If you didn't need to display anything you could just send a location header, but PHP sends all of the headers as soon as you send any output, so if you need to send output you won't be able to send headers after that. You could also use Javascript to do the redirect instead of a meta tag, e.g.:

if ($userlevel != "Administrator"){  echo '<script type="text/javascript">		   alert("YOU ARE NOT AUTHORIZED TO ACCESS THIS PAGE.");		   window.location.href = "WOTCSignin.php"; // technically supposed to be a fully-qualified URL		  </script>';}

That will work because the alert box is synchronous, it will pause Javascript execution until you click OK. That may fix the duplicate alert box, I'm not sure why you would see 2 alerts though.For what it's worth, usually when I'm checking for admin access I'll just redirect, and show the error message on the login page. e.g.:

if ($userlevel != "Administrator"){  header ('Location: login.php?error=' . rawurlencode('You do not have access to that page'));  exit();}

Then on the login page I check to see if $_GET['error'] is empty, and display the error message near the login form somewhere.

Link to comment
Share on other sites

Yeah, it's better to use a meta tag for this since you want to display content on the page (the Javascript code). If you didn't need to display anything you could just send a location header, but PHP sends all of the headers as soon as you send any output, so if you need to send output you won't be able to send headers after that. You could also use Javascript to do the redirect instead of a meta tag, e.g.:
if ($userlevel != "Administrator"){  echo '<script type="text/javascript">		   alert("YOU ARE NOT AUTHORIZED TO ACCESS THIS PAGE.");		   window.location.href = "WOTCSignin.php"; // technically supposed to be a fully-qualified URL		  </script>';}

That will work because the alert box is synchronous, it will pause Javascript execution until you click OK. That may fix the duplicate alert box, I'm not sure why you would see 2 alerts though.

Right again; as if there were ever a doubt. I added the window code as above, and Voila! the page went where it was supposed to go, and the alert box only appeared once.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...