Welcome to The Forum

Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads

PHP Help needed


Tommo
 Share

Recommended Posts

So hi,

 

Currently i'm trying to write to this file on my webserver. Everything works as it should, besides the fact that every time i use a apostrophe or a quotation mark there appears a back slash.

 

ex. I type I'm -> It comes back I\'m

or

ex. class="hi" -> class=\"hi\"

 

I tried putting in the stripslashes() function, but i kept getting error messages. Also, usually when you use stripslashes it also only removes the backslashes from view, but they're actually still there in the file. That's why it needs to be able to completely clean from the backslashes in the actual textfile. The text in this file (the textfile.txt) is going to be include()'d on a webpage.

 

It is going to work as an online editor for some sort of navigation system i'm working on for shits and giggles. But i'm currently stuck with.

(Also, i haven't written this script myself, but i have found it like this on the internet)

 

<?php
if($_POST['Submit']){
$open = fopen("textfile.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />"; 
echo "File:<br />";
$file = file("textfile.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.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>";
}
?> 

 

Here's a running version on my host.

http://php.utommo.com/writetext2/

Link to comment
Share on other sites

You really don't need to use $PHP_SELF

action=""

That will post to the current page, and if you want a record that something actually happened (if the page refresh wasn't enough)

action="#"

That will append a # at the end of the URL to show the form was submitted.

 

<?php 
if($_POST['Submit']){ 
$open = fopen("textfile.txt","w+"); 
$text = stripslashes(stripslashes($_POST['update'])); 
fwrite($open, $text); 
fclose($open); 
echo "File updated.<br />";  
echo "File:<br />"; 
$file = file("textfile.txt"); 
foreach($file as $text) { 
echo $text . "<br />"; 
} 
}else{ 
$file = file("textfile.txt"); 
echo '<form action="" 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>'; 
} 
?>

 

That should fix it...but you probably should recursively stripslashes or use regex to get it -.-

Edited by Papa John
Link to comment
Share on other sites

You really don't need to use $PHP_SELF

action=""

That will post to the current page, and if you want a record that something actually happened (if the page refresh wasn't enough)

action="#"

That will append a # at the end of the URL to show the form was submitted.

 

<?php 
if($_POST['Submit']){ 
$open = fopen("textfile.txt","w+"); 
$text = stripslashes(stripslashes($_POST['update'])); 
fwrite($open, $text); 
fclose($open); 
echo "File updated.<br />";  
echo "File:<br />"; 
$file = file("textfile.txt"); 
foreach($file as $text) { 
echo $text . "<br />"; 
} 
}else{ 
$file = file("textfile.txt"); 
echo '<form action="" 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>'; 
} 
?>

 

That should fix it...but you probably should recursively stripslashes or use regex to get it -.-

 

Thank you so much! So far it looks like it is doing what i need it to do. So i will continue to mess around with it.

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

 Share