Jump to content

display php variable in alert box


xerox123

Recommended Posts

This is my code

 


$file = $file . $line;
                fclose($fh);
                echo "<script language= 'JavaScript'>alert(' . $file . ');</script>";

 

the alert box is not coming up.  :confused:

 

Please suggest a way to print the contents of the file in a alert box. :-\

 

 

 

Link to comment
Share on other sites

does alert function have to defined in the php page? :shrug:

 

alert is pre-defined rite?

 

echo "<script type='text/javascript'>alert('{$variabl}');</script>

 

the alert box does not appear...

 

alert if a Java Script function, so it doesn't have to be defined on the PHP page. And what he said works fine besides the fact that he forgot the ending quote after </script>

 

<?php
$Text = "Hello World!";
echo "<script type='text/javascript'>alert('{$Text}');</script>"
?>

Link to comment
Share on other sites

does alert function have to defined in the php page? :shrug:

 

alert is pre-defined rite?

 

echo "<script type='text/javascript'>alert('{$variabl}');</script>

 

the alert box does not appear...

 

alert if a Java Script function, so it doesn't have to be defined on the PHP page. And what he said works fine besides the fact that he forgot the ending quote after </script>

 

<?php
$Text = "Hello World!";
echo "<script type='text/javascript'>alert('{$Text}');</script>"
?>

 

haha we both messed up, you forgot a ; at the end..

 

sorry btw @OP I can't really see too well you have to pardon my crappy help :)

 

<?php
$Text = "Hello World!";
echo "<script type='text/javascript'>alert('{$Text}');</script>";
?>

Link to comment
Share on other sites

does alert function have to defined in the php page? :shrug:

 

alert is pre-defined rite?

 

echo "<script type='text/javascript'>alert('{$variabl}');</script>

 

the alert box does not appear...

 

alert if a Java Script function, so it doesn't have to be defined on the PHP page. And what he said works fine besides the fact that he forgot the ending quote after </script>

 

<?php
$Text = "Hello World!";
echo "<script type='text/javascript'>alert('{$Text}');</script>"
?>

 

haha we both messed up, you forgot a ; at the end..

 

sorry btw @OP I can't really see too well you have to pardon my crappy help :)

 

<?php
$Text = "Hello World!";
echo "<script type='text/javascript'>alert('{$Text}');</script>";
?>

 

Ooops, well that's embarrassing :P

Link to comment
Share on other sites

The example you provided is running fine.

 

However, I want to display the contents of a file. :(

 

<?php
$fh = fopen("example.txt", "r");
              while (!feof($fh))
              {
                       $line = fgets($fh);
                       $file = $file . $line;
              }
                fclose($fh);
echo "<script type='text/javascript'>alert('{$file}');</script>";
?>

 

but the alert box is not appearing...can u suggest sumthin?  :'(

Link to comment
Share on other sites

The example you provided is running fine.

 

However, I want to display the contents of a file. :(

 

<?php
$fh = fopen("example.txt", "r");
              while (!feof($fh))
              {
                       $line = fgets($fh);
                       $file = $file . $line;
              }
                fclose($fh);
echo "<script type='text/javascript'>alert('{$file}');</script>";
?>

 

but the alert box is not appearing...can u suggest sumthin?  :'(

 

It seems that it only works if there's only one line in the file. Maybe the hidden \r\n character (it is used in edit controls in the C++ Win32 API, which is what Notepad.exe was made in, for a new line) in the text is messing it up. Because I can echo it to the screen fine:

 

<?php
$fh = fopen ("example.txt", "r");
while (!feof ($fh))
{
     $file = $file . fgets ($fh) . "</br>";
}
fclose ($fh);
echo $file;
?>

 

Just not use the alert function =/

 

So, not sure on this one, maybe someone else can help you out.

Link to comment
Share on other sites

Here's a quick example I just created. It uses the jQuery Javascript library to do the AJAX functions. jQuery makes AJAX relatively painless. :) It also uses the jQuery UI functions to display the modal window. The text file it's displaying is just random text from http://www.lipsum.com/

 

You can see it in action at http://rbnsn.com/phpfreaks/ajax_display_file.php

 

<?php
if (isset($_POST['d'])) {
if (!file_exists('test.txt')) {
	exit(json_encode(array('ret'=>'Not OK','error'=>'Oops - file not found')));
}
$cnts = file_get_contents('test.txt');
exit(json_encode(array('ret'=>'ok','filename'=>'test.txt','contents'=>nl2br($cnts))));
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
	<title>AJAX Show File</title>
	<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" type="text/css">
	<style type="text/css">
		html, body {
			margin: 0;
			border: 0;
			font-size:100%;
			font-family: sans-serif;
		}
	</style>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
	<script type="text/javascript">
		$(document).ready(function() {
			var vph = $(window).height();
			var vpw = $(window).width();
			$("#message").dialog({
					bgiframe: true,
					autoOpen: false,
					width: vpw - 100,
					height: vph - 100,
					modal: true,
					buttons: {
						'Ok': function() {
							$(this).dialog('close');
						}
					}
			});
			$('#show').click(function() {
				$.post("<?php echo $_SERVER['PHP_SELF']; ?>",{d:1},
					function(data) {
						if (data.ret == 'Not Ok') {
							$("#message").dialog( "option", "title","Error Found");
								$('#message').html('<p>' + data.error + '</p>');
								$('#message').dialog('open');
								return false;
						} else {
							$("#message").dialog( "option", "title", data.filename);
							$("#message").html(data.contents);
							$('#message').dialog('open');
							return false;
						}
					},
				'json');
			});
		})
	</script>
</head>
<body>
	<button id="show">Show File</button>
	<div id="message"></div>
</body>
</html>

 

Ken

Link to comment
Share on other sites

;)That's quite fancy... I was looking for something more simpler, like displaying the contents in textarea...

 

The button that should display my alert box has two action....i wonder whether that is the problem why alert box does not appear....

 

When the button is clicked the alert box should be displayed followed by another pop-box for downloading a file.....

 

This is the sequence of events:

 

1) Button is pressed

2) Script is run to generate a file

3) Some of the contents of the file need to be masked before they can be displayed

4) File contents is displayed in pop-box

5) pop-box is closed

6) Download box appears to download the same file

 

All these events have to take place on one button click. Maybe the alert box does not appear because there are so many event taking place on one button click.

 

Do you suppose there is problem in having so many events in one button click?  :shrug:

 

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.