Dzień dobry.
Bawię się w pisanie prostego systemu, zapisującego treść do pliku php. Jednak, z jakichś powodów strona nie działa a ja nie wiem dlaczego, debuger nie działa
Podam kod:
index.php
<html>
<body>
<script>
function sendXML(location, strParams, redirectToOnSuccess)
{
var xmlhttp;
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange =
function ()
{
if (this.readyState == 4 && this.status == 200)
if (redirectToOnSuccess != "")
{
alert("Operacja zakończona sukcesem");
window.open(redirectToOnSuccess, "_self");
}
}
xmlhttp.open("POST", location, true);
xmlhttp.send(strParams);
}
document.addEventListener("click", function (event)
{
var authors = ["Zbyszek", "Zosia", "Franek", "Hubert", "Łukasz"];
var fd = new FormData();
fd.append("date1", "" + Math.abs(Math.ceil((Math.random() - 0.5) * 4000) + " rok"));
fd.append("author1", authors[Math.floor(Math.random() * 5)]);
fd.append("topic1", "Temat testowy");
fd.append("body1", "Jakas tresc");
sendXML("db_before.php", fd, "index.php");
});
</script>
<?php
include "db.php";
foreach ($db as $entry)
echo "<p>Data: " . $entry["date"] . ", Autor: " . $entry["author"] . "<br>Temat: " . $entry["topic"] . "<br>Treść: " . $entry["body"] . "</p>";
?>
</body>
</html>
mi.php
<?php
$maxIndex = -1;
?>
db_before.php
<?php
$dDate = $_POST["date1"];
$aAuth = $_POST["author1"];
$tTopi = $_POST["topic1"];
$bBody = $_POST["body1"];
include "mi.php";
global $maxIndex;
$maxIndex++;
$newMaxIndexString = "<?php \$maxIndex = $maxIndex; ?>";
$entry = "array(\"date\" => "\"$dDate\", \"author\" => "\"$aAuth\", \"topic\" => "\"$tTopi\", \"body\" => "\"$bBody\"),\n";
$dbString = file_get_contents("db.php");
$dbString = str_replace("/*tu*/", "\"" . strval($maxIndex) . "\" => " . $entry . "/*tu*/", $dbString);
file_put_contents("db.php", $dbString);
file_put_contents("mi.php", $newMaxIndexString);
?>
db.php
<?php
$db = array
(
/*tu*/
);
?>
Czy widzicie tu jakiś błąd? czemu ko uruchomieniu index.php i kliknięciu w obszarze roboczym, nie dodają się nowe pozycje?
Dzięki
M.