marketease Posted January 6, 2009 Share Posted January 6, 2009 Hi, I'm fairly new to php, and I am having a problem with updating a txt file using php. We are building some template websites, and we want customers to be able to update the text in some areas. The text in those areas is read from a txt file that can be changed using a text area and submit form in the admin area. The problem is that for certain characters, such as quotation (") marks, php puts a slash (\) in front of the character. I understand that this is part of the syntax of php, but how do I get it to echo exactly what is put in the text area, and not add a slash before syntax characters like quotes? Here is my code. "hometext.txt" is the text file that is being written to and read from. <? if($_POST['Submit']){ $open = fopen("hometext.txt","w+"); $text = $_POST['update']; fwrite($open, $text); fclose($open); echo "File updated.<br />"; echo "File:<br />"; $file = file("hometext.txt"); foreach($file as $text) { echo $text."<br />"; } echo "<p><a href=\"../indexDynamic.php\">click here to view your changes live</a></p>"; echo "<p><a href=\"../upload\">click here to make more changes</a></p>"; }else{ $file = file("hometext.txt"); echo "<form action=\"".$PHP_SELF."\" method=\"post\">"; echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">"; foreach($file as $text) { echo $text; } echo "</textarea>"; echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n </form>"; } ?> Any help would be much appreciated! If I should use a different method other than reading from a txt file, let me know. Quote Link to comment Share on other sites More sharing options...
DamienRoche Posted January 6, 2009 Share Posted January 6, 2009 First, it's much better to weave in and out of the php. So instead of echoing the html, just do this: } ?> <p><a href="../indexDynamic.php">click here to view your changes live</a></p> <p><a href="../upload">click here to make more changes</a></p> <?php }else{ ------------ I think you will have to use stripslashes($contents); for your problem. Hope that helps. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 First, it's much better to weave in and out of the php. So instead of echoing the html, just do this: } ?> <p><a href="../indexDynamic.php">click here to view your changes live</a></p> <p><a href="../upload">click here to make more changes</a></p> <?php }else{ ------------ I think you will have to use stripslashes($contents); for your problem. Hope that helps. Weaving in and out makes code a huge mess. I would use echo '' with single quotes and not escape the double quotes and remove the \n's from the code, but yea. I actually tend to store my output in a string then display it after processing. But yea, I shutter at the weaving php tags....yuck. And as far as performance, they are about the same. <?php if(isset($_POST['Submit'])){ $open = fopen("hometext.txt","w+"); $text = stripslashes($_POST['update']); fwrite($open, $text); fclose($open); echo 'File updated.<br />'; echo 'File:<br />'; $file = file("hometext.txt"); foreach($file as $text) { echo $text."<br />"; } echo '<p><a href="../indexDynamic.php">click here to view your changes live</a></p>'; echo '<p><a href="../upload">click here to make more changes</a></p>'; }else{ $file = file("hometext.txt"); echo '<form action="'.$_SERVER{'PHP_SELF'].'" method="post">'; echo '<textarea Name="update" cols="50" rows="10">'; foreach($file as $text) { echo $text; } echo '</textarea>'; echo '<input name="Submit" type="submit" value="Update" /></form>'; } ?> Fixed some outdated issues, first added the stripslashes then changed $PHP_SELF to be $_SERVER['PHP_SELF'] then for the heck of it changed the echos to ' instead of " not to mention I properly indented your code. I would get in the habit of properly indenting code. It makes debugging much easier. Quote Link to comment Share on other sites More sharing options...
DamienRoche Posted January 6, 2009 Share Posted January 6, 2009 I do First, it's much better to weave in and out of the php. So instead of echoing the html, just do this: } ?> <p><a href="../indexDynamic.php">click here to view your changes live</a></p> <p><a href="../upload">click here to make more changes</a></p> <?php }else{ ------------ I think you will have to use stripslashes($contents); for your problem. Hope that helps. Weaving in and out makes code a huge mess. I would use echo '' with single quotes and not escape the double quotes and remove the \n's from the code, but yea. I actually tend to store my output in a string then display it after processing. But yea, I shutter at the weaving php tags....yuck. And as far as performance, they are about the same. <?php if(isset($_POST['Submit'])){ $open = fopen("hometext.txt","w+"); $text = stripslashes($_POST['update']); fwrite($open, $text); fclose($open); echo 'File updated.<br />'; echo 'File:<br />'; $file = file("hometext.txt"); foreach($file as $text) { echo $text."<br />"; } echo '<p><a href="../indexDynamic.php">click here to view your changes live</a></p>'; echo '<p><a href="../upload">click here to make more changes</a></p>'; }else{ $file = file("hometext.txt"); echo '<form action="'.$_SERVER{'PHP_SELF'].'" method="post">'; echo '<textarea Name="update" cols="50" rows="10">'; foreach($file as $text) { echo $text; } echo '</textarea>'; echo '<input name="Submit" type="submit" value="Update" /></form>'; } ?> Fixed some outdated issues, first added the stripslashes then changed $PHP_SELF to be $_SERVER['PHP_SELF'] then for the heck of it changed the echos to ' instead of " not to mention I properly indented your code. I would get in the habit of properly indenting code. It makes debugging much easier. Sorry, yeh, I should have realized. I think it depends how html heavy the page is that you are coding. Plus, no way would I use echo all the time while using an ide - hardly any ide treats the echoed html like html. Each to their own I guess. Quote Link to comment Share on other sites More sharing options...
marketease Posted January 7, 2009 Author Share Posted January 7, 2009 Thanks guys! That was exactly what I needed. I'll try to work on my indenting, but that really helped! Quote Link to comment 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.