Current location: Hot Scripts Forums » Programming Languages » PHP » Preview text from a TEXtAREA using PHP??


Preview text from a TEXtAREA using PHP??

Reply
  #1 (permalink)  
Old 03-01-06, 03:08 PM
KeYBLeR KeYBLeR is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
Preview text from a TEXtAREA using PHP??

Hello

I have html page, that looks like this
Code:
<form action=handle.php method=post>
<textarea name=code></textarea>
<input type=button value=Preview>
</form>
How can I make the preview button, open what ever has been entered into the textarea.

Code:
<textarea name=code><font size=4>I Want To Preview This</font></textarea>
Basicly The people are going to type there html code into the textarea, and then I would like for a new window to open, containing a preview of what was just entered into the textarea.

Is this possible?

Thanks keith
Reply With Quote
  #2 (permalink)  
Old 03-01-06, 03:28 PM
stormshadow's Avatar
stormshadow stormshadow is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 355
Thanks: 0
Thanked 0 Times in 0 Posts
easy :-)

$preview = $_POST['code'];
echo"Your Post/Code:<br>
$preview";

:-) Happy learning.
Reply With Quote
  #3 (permalink)  
Old 03-01-06, 03:44 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Try the following instead:

PHP Code:

<?php
echo '<center>';
if(isset(
$_POST['submit']))
{
  echo 
'<table bgcolor="#efefef" width="500"><tr><td>';
  echo 
stripslashes($_POST['stuff']);
  echo 
'</td></tr></table>';
}
echo 
'<br><br><form action="'.$_SERVER['PHP_SELF'].'" method="post">';
echo 
'<textarea name="stuff" cols="60" rows="10">';
if(isset(
$_POST['stuff']))
{
  echo 
stripslashes($_POST['stuff']);
}
echo 
'</textarea><br>';
echo 
'<input type="submit" name="submit" value="preview"></center>';
?>
A Warning! Do NOT use this code to submit user content without validating first! I didn't include any validation in this code, but make sure to do so before actually using it on a real site.
Attached Files
File Type: zip preview.zip (403 Bytes, 192 views)
__________________
Reply With Quote
  #4 (permalink)  
Old 03-02-06, 06:43 AM
4saken 4saken is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
try the following:
Code:
<form action=handle.php method=post>
<textarea id="code" name=code></textarea>
<input type=button value=Preview onclick="doPreview()">
</form>
<script>
function doPreview() {
  var newWin = window.open("", "_blank");
 newWin.document.write("<html><body>"+document.getElementById('code').value+"</body></html>");
  newWin.document.close();
}
</script>
__________________
XPCMS - Everyday CMS News
Reply With Quote
  #5 (permalink)  
Old 03-02-06, 08:54 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Nice code 4saken! Although its in JavaScript, not PHP.
__________________
Reply With Quote
  #6 (permalink)  
Old 03-02-06, 09:01 AM
4saken 4saken is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by digioz
Nice code 4saken! Although its in JavaScript, not PHP.
Thanks
I don't think there is any sense in using PHP script there to just show a preview page like KeYBLeR asked. Javascript will work faster.
__________________
XPCMS - Everyday CMS News
Reply With Quote
  #7 (permalink)  
Old 03-02-06, 09:11 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
I agree. JavaScript is a bit more handy for preview. I would still recommend doing some validation on the user input before displaying the page though.
__________________
Reply With Quote
  #8 (permalink)  
Old 03-02-06, 09:18 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
One other idea is to use the readonly attribute on the textarea. That prevents the user from modifying it.

Here are two other methods.

Code:
<textarea readonly cols=120 rows=25 wrap="off" >
<?php readfile("preprocess.php");?>
</textarea>
PHP Code:

<?php

$buf
=@file_get_contents($filename);
?>
<textarea readonly rows="20" class="textbox" cols="40" name="updated" id="updated" >
<?=$buf?>
</textarea>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM
PHP Runner , Preview PHP files ! moslehi@gmail.com General Advertisements 3 12-08-04 03:01 PM
Form -> text file -> php variables Bonzo PHP 1 06-16-04 07:37 AM
MySQL Query problem Wraith PHP 5 03-06-04 05:16 PM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM


All times are GMT -5. The time now is 06:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.