Jump to content

Edit Text File Php Script


Recommended Posts

I made this script.

<form method="post" enctype="multipart/form-data">     <select name="file" style="width: 150px">          <option value="text1.txt">text1.txt</option>          <option value="text2.txt">text2.txt</option>     </select>     <input type="submit" name="load" value="Load"></form><?phpif (isset($_POST['load']))  {   $fn = ($_POST['file']);   include('box.php');  } if (isset($_POST['save']))  {   $fn = ($_POST['file']);   $content = stripslashes($_POST['content']);   $fp = fopen($fn,"w"); //or die ("Error opening file in write mode!");   fputs($fp,$content);   fclose($fp); //or die ("Error closing file!");  }?>

and this box.php

<form  method="post" enctype="multipart/form-data">     <textarea name="content" style="width: 600px; height: 300px;"><?php readfile($fn) ?></textarea><br />     <input type="submit" name ="save" value="Save"></form>

the script works fine until when comes to saving the data. It gives me this errors. Notice: Undefined index: file in /var/www/html/daniel/phptextedit/index.php on line 19 Warning: fopen(): Filename cannot be empty in /var/www/html/daniel/phptextedit/index.php on line 21 Warning: fputs() expects parameter 1 to be resource, boolean given in /var/www/html/daniel/phptextedit/index.php on line 22 Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/daniel/phptextedit/index.php on line 23 Anyone can help me to solve these problems?

Edited by dmallia
  • Like 2
Link to post
Share on other sites

The form that is being submitted does not have an item called "file" in it. If you submit the one form, then show another form and submit that, then you need to put the information from the first form into the second one. You can use hidden inputs for that.

Link to post
Share on other sites
The form that is being submitted does not have an item called "file" in it. If you submit the one form, then show another form and submit that, then you need to put the information from the first form into the second one. You can use hidden inputs for that.
Can you explain in little bit more detail and if possible with code because i didn't understand.
Link to post
Share on other sites

If you submit the first form, the one with a submit button called "load", then it submits a value called "file" with the form and displays the second form. When you submit the second form, the one with a submit button called "save", there is no item called "file" in that form. That form only contains an item called "content". If you want to include the "file" value from the first form onto the second form then you need to add that value to the second form, so add a new input element to the second form and write the value from the first form there. If you use an input element with a type of "hidden" then it won't appear in the browser. You're already using PHP to write the contents of the selected file into a textarea, and you also need to use PHP to write the value from the first form into a hidden input.

Link to post
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.

Loading...
×
×
  • Create New...