xerox123 Posted March 17, 2011 Share Posted March 17, 2011 This is my code $file = $file . $line; fclose($fh); echo "<script language= 'JavaScript'>alert(' . $file . ');</script>"; the alert box is not coming up. Please suggest a way to print the contents of the file in a alert box. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/ Share on other sites More sharing options...
RussellReal Posted March 17, 2011 Share Posted March 17, 2011 echo "<script type='text/javascript'>alert('{$variabl}');</script> Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1188596 Share on other sites More sharing options...
xerox123 Posted March 17, 2011 Author Share Posted March 17, 2011 does alert function have to defined in the php page? alert is pre-defined rite? echo "<script type='text/javascript'>alert('{$variabl}');</script> the alert box does not appear... Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1188600 Share on other sites More sharing options...
WriteGreatCode Posted March 17, 2011 Share Posted March 17, 2011 does alert function have to defined in the php page? 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>" ?> Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1188604 Share on other sites More sharing options...
RussellReal Posted March 17, 2011 Share Posted March 17, 2011 does alert function have to defined in the php page? 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1188607 Share on other sites More sharing options...
WriteGreatCode Posted March 17, 2011 Share Posted March 17, 2011 does alert function have to defined in the php page? 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 Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1188615 Share on other sites More sharing options...
xerox123 Posted March 18, 2011 Author Share Posted March 18, 2011 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? :'( Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1188991 Share on other sites More sharing options...
WriteGreatCode Posted March 18, 2011 Share Posted March 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1189054 Share on other sites More sharing options...
kenrbnsn Posted March 18, 2011 Share Posted March 18, 2011 What are you trying to accomplish with this code? Since we're seeing only a small snippet of the script, we can't suggest a (possibly) better way. Ken Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1189144 Share on other sites More sharing options...
xerox123 Posted March 21, 2011 Author Share Posted March 21, 2011 I want to read the contents of a file, line by line, edit some lines and display the whole contents of the file using an alert box. Or a pop-up box. This pop-up box or alert box should come up after clicking a button. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1190132 Share on other sites More sharing options...
kenrbnsn Posted March 21, 2011 Share Posted March 21, 2011 Your best way of accomplishing this is via Javascript with AJAX. Ken Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1190146 Share on other sites More sharing options...
xerox123 Posted March 21, 2011 Author Share Posted March 21, 2011 can you provide an example? Where the contents of a file are read...and displayed..... Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1190153 Share on other sites More sharing options...
kenrbnsn Posted March 21, 2011 Share Posted March 21, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1190160 Share on other sites More sharing options...
xerox123 Posted March 21, 2011 Author Share Posted March 21, 2011 ;)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? Quote Link to comment https://forums.phpfreaks.com/topic/230898-display-php-variable-in-alert-box/#findComment-1190171 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.