0

So i have this code, but i've tried a lot but can't get the form working, no idea why. And stackoverflow is always the place where to go for help, that's why I'm here.

I need a submit form so it adds the text to the file

This is the code to insert the text , in this case, an update.

<?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";
}

?>

This is the code to display the text, in this case, the update file

<?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;
?>
  • 2
    What's not working? Any error messages? Did you Google them? – Mike Jul 26 '16 at 06:52
  • I'm new to php, so i didn't know what to search to be honest. Im sorta a fresh meat in this language, and this isn't my code, but i tried added the form into the print "input update here", but that didn't work. @Mike – Rūdolfs Naumenko Jul 26 '16 at 06:54
  • See this question first: http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Mike Jul 26 '16 at 06:55
  • There's no error, just because there's no error like to apear. i cant add the update, because there's no
    . The thing i wanted to know how to make the form so it inputs the update and triggers the script. @Mike
    – Rūdolfs Naumenko Jul 26 '16 at 07:01

0 Answers0