JSFiddle - React, Tailwind, and code Playground
HTML
//filmer_php.php
<?php $thisPage="Filmer (PHP)"; ?>
<?php include 'phpincludes\header.php';?>
<?php include 'phpincludes\filmer_php_process_data.php';?>
<script type="text/javascript" src="js/filmer_php.js"></script>
<h3 id=pageheader class="imagify subtitle">Mina filmer</h2>
<hr id=hrPageheader>
<div id=divMenu class=maindivs>
<form id=formFilmerPHP action="<?php $_PHP_SELF ?>" method=POST>
<fieldset>
<legend>Lägg till en film</legend>
<label>Titel:</label>
<br>
<input id=inputTitle name=title></input>
<br>
<label>Ditt betyg:</label>
<br>
<select id=selectRating name=rating>
<option>-- Välj Betyg --</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<br>
<label>Länk till IMDb:</label>
<br>
<input id=inputLink name=link></input>
<br>
<label>Länk till bild:</label>
<br>
<input id=inputImage name=image></input>
<br>
<label>Filmens handling:</label>
<br>
<textarea id=textareaDescription class=textareaNoResize name=desc></textarea>
<br>
<hr>
<div class=divCenteredButtons>
<input id=btnAddContent type=submit class=buttonsFilmer value="Lägg till filmen"></input>
</div>
</fieldset>
</form>
</div>
<div id=divContentPHP class=maindivs>
<label id=labelContentHeader>Filmer</label>
<ul id=ulContentList>
<?php loadContent("none");?>
</ul>
</div>
<?php include 'phpincludes\footer.php';?>
JavaScript
//filmer_php_process_data.php
<?php
$content_array = array();
if (isset($_POST["title"]))
{
saveContent();
refreshPage();
}
function refreshPage()
{
header('Location: '.$_SERVER['PHP_SELF']);
exit();
}
function saveContent()
{
loadContent("no-update");
$title = $_POST["title"];
$rating = $_POST["rating"];
$link = $_POST["link"];
$image = $_POST["image"];
$desc = $_POST["desc"];
$array = array($title, $rating, $link, $image, $desc);
array_push($GLOBALS["content_array"], $array);
$string = processArrayForSave($GLOBALS["content_array"]);
saveToFile("res/movies.txt", $string);
}
function saveToFile($filename, $string)
{
$handle = fopen($filename, 'w'); // might need binary mode "b",
fwrite($handle, $string);
fclose($handle);
}
function processArrayForSave($array)
{
$string = "";
$j = 1;
$count = count($array);
foreach($array as $i)
{
$string .= implode(";", $i);
if ($j<$count)
$string .= "\n";
$j++;
}
return $string;
}
function loadFromFile($filename)
{
$handle = fopen($filename, 'r'); // might need binary mode "b",
$readin = fread($handle, filesize($filename));
fclose($handle);
return $readin;
}
function loadContent($modifier)
{
$readin = loadFromFile("res/movies.txt");
$raw_array = explode("\n", $readin);
$GLOBALS["content_array"] = processArrayForLoad($raw_array);
if ($modifier != "no-update")
updateGUI();
}
function updateGUI()
{
foreach ($GLOBALS['content_array'] as $i)
{
$title = $i[0];
$rating = $i[1];
echo "<li class=contentList><a class=contentListLink href=filmer_php.php?title=".urlencode($title)."><span class=spantitle>".$title."</span><span class=spanrating>".asteriskifyRating($rating)."</span></a></li>";
}
}
function asteriskifyRating($rating)
{
$asterisk = "";
for ($i=0; $i<$rating; $i++) {
$asterisk .= "*";
}
return $asterisk;
}
function...