dandaman285 Posted June 24, 2011 Share Posted June 24, 2011 Im new to programming and am having trouble writing some code. I basically want to write the code to save text from a text field to a specified file path, regardless if the user clicks the preview or save button. Also, if the user does click the preview button, I want the textarea field to stay the same, but to also print out what was in the text field. This is my code, and it prints out what was in the text area, but also does some other weird stuff too, and I can't figure out where. <?php $save_file= $_POST['save_file']; $preview= $_POST['preview_file']; $content = $_POST['content']; $loadcontent = "/filepath.txt"; if($save_file) { $content = stripslashes($content); $fp = @fopen($loadcontent, "w"); if ($fp) { fwrite($fp, $content); fclose($fp); } } $fp = @fopen($loadcontent, "r"); $loadcontent = fread($fp, filesize($loadcontent)); $loadcontent = htmlspecialchars($loadcontent); fclose($fp); if($preview) { $content = stripslashes($content); $fpreview = @fopen($loadcontent, "w"); if ($fpreview) { fwrite($fpreview, $content); fclose($fpreview); } print $content; } ?> <form method=post action="<?=$SERVER['{PHP_SELF']?>"> <textarea name="content" cols="60" rows="25"><?=$loadcontent?></textarea><br/> <input type= "submit" name = "save_file" value="Submit/Save"> <input type= "submit" name = "preview_file" value="Preview"> </form> Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/240295-form-previewsave-help/ Share on other sites More sharing options...
joel24 Posted June 24, 2011 Share Posted June 24, 2011 but also does some other weird stuff too, and I can't figure out where. what other weird stuff does it echo...? and what part do you want help with. Link to comment https://forums.phpfreaks.com/topic/240295-form-previewsave-help/#findComment-1234265 Share on other sites More sharing options...
dandaman285 Posted June 24, 2011 Author Share Posted June 24, 2011 So it prints out what was in the text box perfectly fine, but then it changes what was inside of the text box if you preview it more than once. Specifically, if you preview it once, it will print it out and keep what was in the text box. But then if you preview you the text box after changing some of the text, it will still print whatever was in the text box, BUT what is in the text area will change back to what was previously previewed the first time. I just want to always keep whats inside the text area inside the text area, and have a preview feature print whatever is inside of it. Im sorry if this is hard to understand. Thanks Link to comment https://forums.phpfreaks.com/topic/240295-form-previewsave-help/#findComment-1234319 Share on other sites More sharing options...
EdwinPaul Posted June 24, 2011 Share Posted June 24, 2011 $save_file and $preview are not booleans. You have to compare them with something. How does your script know wether a button has been pressed? Like this: if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) { And do NOT use: <form method=post action="<?=$SERVER['{PHP_SELF']?>"> but use: <form method="post" action=""> Link to comment https://forums.phpfreaks.com/topic/240295-form-previewsave-help/#findComment-1234323 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.