Jump to content

Outputting Variables in an Alert Box (or something similar)


OtagoHarbour
 Share

Recommended Posts

I have a function that allows the user to upload files. The files are then checked to see if they are valid (appropriate to my application). If some of the files are invalid, I would like an alert box (or something similar) to pop up and list the files that are invalid. I try the following code.

		// Make list of invalid file names (w/o the search path)		$Str="The following files appear to be invalid.</p>";		for ($i = 1; $i <= $fileCount; $i++)			if (strstr($status[$i],"I"))			{				$Str=$Str . strrchr($fileName[$i], '\\') . "<br \>";			}				// Output message in alert box		?>		<script type="text/javascript">			var message = <?= $Str ?>;			alert(message);		</script>		<?php

but it does not work.I would be most grateful if someone could tell me what I am doing wrong and how I can display the string $Str in some sort of pop-up box.Many thanks in advance,Peter.

Link to comment
Share on other sites

The string has to be quoted. And I don't recommend PHP's short syntax due to compatibility across servers; Use echo instead:var message = "<?php echo $Str ?>";

Link to comment
Share on other sites

The string has to be quoted. And I don't recommend PHP's short syntax due to compatibility across servers; Use echo instead:var message = "<?php echo $Str ?>";
Thanks very much. That worked in that it displayed the string in an alert box. I also see your point about the PHP short syntax.One remaining problem is that the <br \> and </p> are output literally rather than giving me line breaks. That is I getThe following files appear to be invalid.</p>file1.txt<br \>file2.txt<br \>instead ofThe following files appear to be invalid.file1.txtfile2.txtIf I use \n instead, as in,
		$Str="The following files appear to be invalid.\n\n";		for ($i = 1; $i <= $fileCount; $i++)			if (strstr($status[$i],"I"))			{				$Str=$Str . strrchr($fileName[$i], '\\') . "\n";			}

I don't get anything which presumably means it causes a JavaScript error.Thanks,Peter.

Link to comment
Share on other sites

You might want to reconsider your plan. The way it is now:1. user uploads a file from upload page2. server validates files3. alert page notifies user there is a problem4. User must find a way to return to upload pageA more friendly solution would redirect to the upload page and present a message that the file did not validate. This way, the user doesn't have to go through step 4. Think about sites you've visited where, for example, you create an account. If your username is already used, or you read the capcha incorrectly, you come immediately back to the creation page. (And there are no alert boxes!) That is the kind of experience you want to create.

Link to comment
Share on other sites

HTML isn't parsed in alert boxes. If you want a line break you can use \n
Thanks. That seems to work except I seem to have a problem when I pass a string containing \n into JavaScript usingvar message = "<?php echo $Str ?>";I tried passing the html code into JavaScript and then replacing it with \n using
			var message = "<?php echo $Str ?>"; 			message.replace("</p>", "\n");			message.replace("<br \>", "\n");			alert(message);

but the html code remains in the string and is not replaced by \n.Thanks,Peter.

Link to comment
Share on other sites

You might want to reconsider your plan. The way it is now:1. user uploads a file from upload page2. server validates files3. alert page notifies user there is a problem4. User must find a way to return to upload pageA more friendly solution would redirect to the upload page and present a message that the file did not validate. This way, the user doesn't have to go through step 4. Think about sites you've visited where, for example, you create an account. If your username is already used, or you read the capcha incorrectly, you come immediately back to the creation page. (And there are no alert boxes!) That is the kind of experience you want to create.
Thanks. Yes. The way my code is set up is that the user is returned to the menu from which (s)he can select the upload page from the menu. I could change it so that (s)he is redirected straight back to the upload page instead. It is set up so that some files may be valid and stored under the user specified ID while the invalid files are deleted. The menu allows the user to delete the file set by choosing a "delete set" function from the menu (from which (s)he can also choose the upload page). If the ID is deleted, the files are deleted along with their directory and database record. They may then choose to upload a fresh set. Alternatively, they could choose to just keep the valid files in that set. It's also quite easy to get back to the menu from the upload page.Thanks,Peter.
Link to comment
Share on other sites

it's seem like if you are trying render markup, just embed it into the page for the user as feedback, with the ability to try again (like DD is saying). Alternatively, if the file is successfully uploaded, then you can show a success message instead.

Link to comment
Share on other sites

it's seem like if you are trying render markup, just embed it into the page for the user as feedback, with the ability to try again (like DD is saying). Alternatively, if the file is successfully uploaded, then you can show a success message instead.
It seems to work when I manually put the \n into the alert box. What I can't understand is why the replace function doesn't work with JavaScript. Maybe that's a purely JavaScript question.Thanks,Peter.
Link to comment
Share on other sites

The code you showed replaces only a closing </p> tag. The opening <p> tag will remain. (A good regex can solve the problem.) Also, the <br \> tag is incorrect. Assuming your HTML is correct, replace the <br /> tag instead.

Link to comment
Share on other sites

The code you showed replaces only a closing </p> tag. The opening <p> tag will remain. (A good regex can solve the problem.) Also, the <br \> tag is incorrect. Assuming your HTML is correct, replace the <br /> tag instead.
The <br \> tag is really just a place holder in this case. Ideally is is a string that no one is likely to use in their file name (although that is probably an imaginary ideal these days). My main problem was that I couldn't get the replace function to do what I wanted it to do. Maybe it doesn't really change the string but changes what is output by document.write(). I tried assigning the result to a new string but that seems to be invalid. I did find a solution to my problem. A bit more cumbersome than I thought it would be. This works.
		<script type="text/javascript">			var message = "<?php echo $Str ?>";			array=message.split("<br \>");			ArrayLength=array.length;			message=array[0].concat("\n",array[1]);			if (ArrayLength>1) 			{				for (i=2;i<ArrayLength;i++)				{					message2=message.concat("\n",array[i]);					message=message2;				}			}			alert(message);		</script>

At this point this may be in the wrong forum since this is pure JavaScript.Thanks,Peter.

Link to comment
Share on other sites

Oops. I should have noticed that. string.replace doesn't change the value of string, it returns a new string with the changes. Your solution is very cumbersome, as you say. The normal thing would look like this:message = message.replace(/<br \>/g, "\n");EDIT: multiple replacements need a regular expression.

Edited by Deirdre's Dad
Link to comment
Share on other sites

Oops. I should have noticed that. string.replace doesn't change the value of string, it returns a new string with the changes. Your solution is very cumbersome, as you say. The normal thing would look like this:message = message.replace(/<br \>/g, "\n");EDIT: multiple replacements need a regular expression.
That works and is a lot less cumbersome as you say. Thanks very much! I like short code since I usually need to revisit it at some point. Plus the solution you gave is probably optimized. I thought that the assignment operation was invalid since I tried it and did not get anything (which I took to mean a bug) but the bug must have been elsewhere.Thanks again,Peter.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...