/    Sign up×
Community /Pin to ProfileBookmark

Edit Text File

I made this script.

[B]redakcia.php[/B]

[code=php]<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!”);
}
?>[/code]

[B]box.php[/B]

[code=php]<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>[/code]

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

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 23.2013 — 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.
Copy linkTweet thisAlerts:
@jouvenetauthorJun 23.2013 — 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.[/QUOTE]


I tried this:

[U]This is an error that shows:[/U]

Notice: Undefined variable: header in D:wwwmagazintestform.php on line 22

[code=php]<?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 .= "rn";$ct=0;}
if($ct != 0 && $value != "SAVE"){$elements .= ",";}
}
$fh = fopen($myFile, 'w');
fwrite($fh,$elements);
fclose($fh);
}
$fh = fopen($myFile, 'r');
$theData = explode("rn",fread($fh, filesize($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>";
?>[/code]


[U]when save the changes, shows these errors:[/U]

Notice: Undefined variable: elements in D:wwwmagazintestform.php on line 10

Notice: Undefined variable: header in D:wwwmagazintestform.php on line 22
Copy linkTweet thisAlerts:
@NogDogJun 23.2013 — Oh, okay: you just need to initialize $header before you start trying to append to it with the .= operator.
[code=php]
$header = ''; // now you have something you can append to
// blah blah blah
$header .= $some_value;
[/code]
×

Success!

Help @jouvenet spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...