Jon
Jon

Reputation: 2584

Text file isn't updating

I have this code:

<?php
if(isset($_POST['submit'])){
$fi = "/home2/chippery/public_html/rtf/webR/top.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["top"];
fwrite($fih,$cont);
fclose($fih);


$fi = "/home2/chippery/public_html/rtf/webR/bottom.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["bottom"];
fwrite($fih,$cont);
fclose($fih);


$fi = "/home2/chippery/public_html/rtf/webR/nextTime.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["time"];
fwrite($fih,$cont);
fclose($fih);
}
?>

<html>
<head>
<title>Chipperyman573</title>
<link rel="shortcut icon" href="/fav.ico" />
</head>
<body>
<form method="post" action="change.html">
  Top: <input type="text" name="top" /><br>
  Bottom: <input type="text" name="bottom" /><br>
  Time (MS): <input type="text" name="time" /><br>
  <input type="submit" value="Save" />
</form>
</body>
</html>

I have already made the text pages, and when I try to use this form the pages don't change. What am I doing wrong? I'm incredibly new to html/php so I'm sure it's something stupid.

Upvotes: 1

Views: 136

Answers (3)

ritesh
ritesh

Reputation: 2265

For writing content in file you should use fwrite() after opening the file in write mode.

And in your code few mistakes are there, updated code is here. I have supposed your file name is myfile.php-

<?php
if(isset($_POST['submit'])){
$fi = "top.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["top"];
fwrite($fih,$cont);
fclose($fih);


$fi = "bottom.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["bottom"];
fwrite($fih,$cont);
fclose($fih);


$fi = "nextTime.txt";
$fih = fopen($fi, 'w');
$cont = $_POST["time"];
fwrite($fih,$cont);
fclose($fih);
}
?>
<html>
<head>
<title>Chipperyman573</title>
<link rel="shortcut icon" href="/fav.ico" />
</head>
<body>
<form method="post" action="myfile.php">
  Top: <input type="text" name="top" /><br>
  Bottom: <input type="text" name="bottom" /><br>
  Time (MS): <input type="text" name="time" /><br>
  <input type="submit" value="Save" />
</form>
</body>
</html>

Upvotes: 3

Vasiliy Gudoshnikov
Vasiliy Gudoshnikov

Reputation: 11

Also you can use the file_put_contents() function.

This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

<?php
if (isset($_POST['submit'])) {
    $fi = "top.txt";
    $cont = $_POST["top"];
    file_put_contents($fi, $cont);


    $fi = "bottom.txt";
    $cont = $_POST["bottom"];
    file_put_contents($fi, $cont);


    $fi = "nextTime.txt";
    $cont = $_POST["time"];
    file_put_contents($fi, $cont);
}
?>

Upvotes: 1

Sterling Archer
Sterling Archer

Reputation: 22405

<?php
if(isset($_POST['submit'])){
$fi = "top.txt";
$fih = fopen($fi, 'w') <-- missing semi-colon
$cont = $_POST["top"] <-- missing semi-colon
fclose($fih);


$fi = "bottom.txt";
$fih = fopen($fi, 'w') <-- missing semi-colon
$cont = $_POST["bottom"];
fclose($fih);


$fi = "nextTime.txt";
$fih = fopen($fi, 'w') <-- missing semi-colon
$cont = $_POST["time"];
fclose($fih);
}
?>

And as Ritesh said, you aren't writing any data to your files. Just opening, and closing them.

Upvotes: 0

Related Questions