GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Feb-2005, 11:39
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough

including html help last time!


Arrg this is really getting to me.
I am trying to include html into a textarea but am still having problems. Would I need to encode the html before including it?
Pls can someone help me out.

PHP Code:

if (file_exists($whichpage))
 {
     echo "<img src=\"images/info.gif\"> <font>Editing page: <a href=\"http://www.sportstar.co.za/".$whichpage."\" target=\"_blank\">$whichpage</a></font><br /><br />\n";
     echo '<form action="admin.php" method="post">';
     echo "<input type=\"hidden\" name=\"whichpage\" value=\"".$whichpage."\">\n";
     echo "<input type=\"hidden\" name=\"stage\" value=\"".($stage+1)."\">\n";
     echo '<center><textarea name="editedtext" style="width:475px;height:400px;">'."\n";
     echo file_get_contents($whichpage);
     echo '</textarea></center><br /><br /><ul><input type="button" value="<< back" class="button" onclick="javascript:history.back(1);"> <input type="submit" value="save" class="button"></ul></form>'."\n";
 } 



So everything works fine until a <textarea> tag is found in the html being displayed.
I use file_get_contents($whichpage); which doesn't encode the html before including it. To fix my error would I need to encode the html?
Thanx in advance
Rich
  #2  
Old 12-Feb-2005, 09:25
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
Well this seems to be a habbit of mine... I've cracked it..
I did need to encode the html and now it works 100% and finally i've gotten over this html inclusion hurdle. I was trying to encode the full string variable (IE from an include_once function) but found the only way to do this was to encode the html line by line.
Here's my encode code to display html in a textarea:

PHP Code:

echo '<center><textarea name="editedtext" style="width:475px;height:400px;">'."\n";
     $filepointer = fopen($whichpage, "r");
     while (!feof($filepointer))
     {
      $line = fgets($filepointer, 4096);
      echo htmlentities($line);
     }
     fclose($filepointer);
     //echo file_get_contents($whichpage);
     echo '</textarea></center>'; 



And here's my code to decode it before saving back to the original file name.

PHP Code:

// method used prior to PHP 4.3.0
// alternatively html_entity_decode can be used
function unhtmlentities($string)
{
 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
 $trans_tbl = array_flip($trans_tbl);
 return strtr($string, $trans_tbl);
}

// delete original file and create a new one with same name and edited html text
function savetext()
{
 GLOBAL $editedtext, $whichpage, $siteurl;
 // decode encoded html text
 $editedtext = unhtmlentities($editedtext);
 if (file_exists($whichpage))
 {
  unlink($whichpage);
  $filepointer = fopen($whichpage,"w+");
  fputs($filepointer, $editedtext);
  fclose($filepointer);
  echo "<img src=\"images/info.gif\"> <font> <a href=\" ".$siteurl.$whichpage."\" target=\"_blank\">$whichpage</a> successfully edited!</font><br /><br />\n";
 }
 else echo "Error: Page $whichpage not found.";
 echo '<ul><input type="button" value="<< back" class="button" onclick="javascript:history.back(1);">';
 echo "<input type=\"button\" value=\"home\" class=\"button\" onclick=\"javascript:parent.location='admin.php';\"></ul>";
} 



I have now officially completed my first simple admin cp! Thanx to gidforums for helping sort out my problems.
  #3  
Old 13-Feb-2005, 17:36
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Rich,

I didn't actually go through the code you pasted inside this thread but I can quickly tell you a couple of things about this subject.

When you want to DISPLAY code, you pass the example code through htmlentities( $code, ENT_QUOTES ).

Then if it was displayed inside a textarea and you POST it to a script to save the example code it's NOT necessary to undo the htmlentities. Try it...
  #4  
Old 14-Feb-2005, 04:08
dopee dopee is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: south africa
Posts: 109
dopee will become famous soon enough
Quote:
Then if it was displayed inside a textarea and you POST it to a script to save the example code it's NOT necessary to undo the htmlentities. Try it...

Yes I remember now from my past trial and errors the server automatically decodes the text (this is set in php.ini?) when it is sent via POST.
I guess I just decoded the text manually for testing purposes in development of the script offline.
Thanx for the reply JdS
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
working with html HELP dopee MySQL / PHP Forum 10 18-Jan-2005 04:31
saving html text dopee MySQL / PHP Forum 1 17-Jan-2005 04:15
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
Re: Programming Techniques WaltP C Programming Language 0 09-Mar-2004 23:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:41.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.