所以我有这个代码,但我已经尝试了很多,但无法使表单工作,不明白为什么。 stackoverflow始终是寻求帮助的地方,这就是我在这里的原因。
我需要一个提交表单,以便将文本添加到文件
这是插入文本的代码,在本例中为更新。
<?php
function getid()
{
global $context;
$userid=$context['user']['name'];
return $userid;
}
$filename = "updates.txt";
function readDataFromFile($filename)
{
$dat = array();
$f = fopen($filename, "r");
while (!feof($f))
{
$dat[] = fgets($f);
}
fclose($f);
return $dat;
}
function rewriteFile($filename, $data)
{
file_put_contents($filename, $data);
}
function addAfter($afterLine, $data)
{
global $rawData;
array_splice($rawData, (int)$afterLine, 0, $data . "\n");
}
@$category = $_POST['category'];
@$update = $_POST['update'];
if(empty($category) && empty($update))
{
print "Please input an update ";
} else
{
$cat = $_POST['category'];
$name = getid();
$upd = $_POST['update'];
$jd=cal_to_jd(CAL_GREGORIAN,date("m"),date("d"),date("Y"));
$date = date("Y/m/d");
$curtime = date('l', strtotime($date)) . ", " . $date;
$timefile = file("updates.txt")[2];
//the data
if(strpos($timefile,$curtime) !== false)
{
$toadd = "- $cat: $upd";// This is what to add to the beginning of the file
$rawData = readDataFromFile("updates.txt");
addAfter(3, $toadd);
rewriteFile("updates.txt", $rawData);
$resource->call ( "requestUpdates",$toadd);
} else
{
$toadd = "\n\n$curtime\n- $cat: $upd";// This is what to add to the beginning of the file
$dat = file_get_contents('updates.txt');
file_put_contents('updates.txt', $toadd . $dat);
}
print "Update Submitted";
}
?>
这是显示文字的代码,在本例中是更新文件
<?php
$main_title = "Updates";
$rawd = file_get_contents("updates.txt");
$rawlines = explode("\n", $rawd);
$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
if (isset($_GET['get_json']))
{
$jsondata = array();
$currentdate = "";
}
$assoc = true;
if (isset($_GET['non_assoc'])) { $assoc = false; }
for ($i = 0; $i < count($rawlines); $i++)
{
$fword = explode(' ', trim($rawlines[$i]));
$fword[0] = ((substr($fword[0], -1) == ",") ? substr($fword[0], 0, -1) : $fword[0]);
if (isset($jsondata))
{
$rawlines[$i] = strip_tags($rawlines[$i], "/");
}
$line = str_replace(array("\n", "\r", "<br />"), "", $rawlines[$i]);
if (in_array($fword[0], $days))
{
if (isset($jsondata))
{
if ($assoc)
{
$jsondata[$line] = array();
}
else
{
$jsondata[sizeof($jsondata)]["date"] = $line;
}
if (empty($currentdate) || $currentdate != $line)
{
$currentdate = $line;
}
}
else
{
$rawlines[$i] = "<span style=\"font-size:15px\"><b>" . $rawlines[$i] . "</b></span>";
}
continue;
}
if (isset($jsondata))
{
if ($assoc)
{
array_push($jsondata[$currentdate], $line);
}
else
{
$jsondata[sizeof($jsondata)]["update"] = $line;
}
}
}
if (!$assoc)
{
}
if (isset($jsondata))
{
die(json_encode($jsondata, JSON_UNESCAPED_SLASHES));
}
$up = "";
foreach ( $rawlines as $item ) {
$up = $up . $item . '<br/>';
}
echo $up;
?>