Jump to content

Form Preview/Save Help


dandaman285

Recommended Posts

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
Share on other sites

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
Share on other sites

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.