Jump to content

Update a txt files rows with a form?


adige

Recommended Posts

yes I know that but I dont want it.I have a text file.

 

What the?! Did you even LOOK at the links he posted? One was on creating forms in PHP and the other was for modifying files. Isn't that what you want to do?

 

I had already completed this before I read that response of yours which suggests you can't even take the time to look at the information provided to you. So, I'll post this anyway, but I do so with hesitation

 

<?php
$fileName = "temp.txt";
$lineToEdit = 10;

if(isset($_POST['editLine']))
{
    $lines = file($fileName, FILE_IGNORE_NEW_LINES);
    $editLine = $_POST['editLine'];
    $lines[$lineToEdit-1] = $editLine;
    $fh = fopen($fileName, 'w') or die("can't open file");
    fwrite($fh, implode("\n", $lines));
    fclose($fh);
}
//Get 10th line from 
if(!isset($line10))
{
    $lines = file($fileName, FILE_IGNORE_NEW_LINES);
    $editLine = (isset($lines[$lineToEdit-1])) ? $lines[$lineToEdit-1] : '';
}

?>
<html>
<body>
<form method="POST">
Value of line <?php echo $lineToEdit; ?>:<br />
<input type="text" style="width:300px;" name="editLine" value="<?php echo $editLine; ?>" /><br /><br />
<button type="submit">Change Value</button>
</form>
<?php
    //This block of code for debugging purposes only
    echo "<pre>\n";
    print_r($lines);
    echo "</pre>\n";
?>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.