www.webdeveloper.com
Results 1 to 4 of 4

Thread: Edit Text File

  1. #1
    Join Date
    Sep 2011
    Posts
    21

    Edit Text File

    I made this script.

    redakcia.php
    PHP Code:
    <form method="post" enctype="multipart/form-data">
         <select name="file" style="width: 150px">
              <option value="magazin1.txt">M 1</option>
              <option value="magazin2.txt">M 2</option>
         </select>
         <input type="submit" name="load" value="Load">
    </form>
    <?php
    if (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!");
      
    }
    ?>
    box.php
    PHP Code:
    <form action="redakcia.php" 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 errors. Anyone can help me to solve these problems?

    how it should be, in my case, the whole form?

    thx ppl

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    Telling us exactly what errors it gives you would probably make it easier for us to help you debug it. Best guess right now is no write permission on the directory/file.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2011
    Posts
    21
    Quote Originally Posted by NogDog View Post
    Telling us exactly what errors it gives you would probably make it easier for us to help you debug it. Best guess right now is no write permission on the directory/file.

    I tried this:
    This is an error that shows:
    Notice: Undefined variable: header in D:\www\magazin\test\form.php on line 22

    PHP Code:
    <?php
    $myFile 
    "magazin.txt";
    $fields = array("Magazin");
    if(!empty(
    $_POST["submit"]))
    {
     
    $ct 0;
     foreach(
    $_POST as $value)
     {
      
    $ct++;
      if(
    $value != "SAVE"){$elements .= $value;}
      if(
    $ct == count($fields)){$elements .= "\r\n";$ct=0;}
      if(
    $ct != && $value != "SAVE"){$elements .= ",";}
      }
     
    $fh fopen($myFile'w');
     
    fwrite($fh,$elements);
     
    fclose($fh);
     }
    $fh fopen($myFile'r');
    $theData explode("\r\n",fread($fhfilesize($myFile)));
    fclose($fh);
    for(
    $i=0;$i<count($theData);$i++){$finalData[] = explode(",",$theData[$i]);}
    for(
    $i=0;$i<count($fields);$i++){$header .= "<th style='border-bottom:2px solid #000;'>$fields[$i]</th>";}
    echo 
    "<form action='#' method='POST'><table border='0' cellpadding='5' rules='cols'><tr>$header</tr>";
    for(
    $i=0;$i<count($finalData);$i++)
    {
     echo 
    "<tr>";
     for(
    $j=0;$j<count($finalData[$i]);$j++){if($finalData[$i][$j]){echo "<td align='center'><input size='80' name='".$fields[$j]."_".$i."' value='".$finalData[$i][$j]."'></td>";}}
     echo 
    "</tr>";
     }
    echo 
    "</table><input type='submit' name='submit' value='SAVE'></form>";
    ?>
    when save the changes, shows these errors:
    Notice: Undefined variable: elements in D:\www\magazin\test\form.php on line 10
    Notice: Undefined variable: header in D:\www\magazin\test\form.php on line 22

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    Oh, okay: you just need to initialize $header before you start trying to append to it with the .= operator.
    PHP Code:
    $header ''// now you have something you can append to
    // blah blah blah
    $header .= $some_value
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles