Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
guestbook/index.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
82 lines (75 sloc)
2.37 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Guestbook</title> | |
</head> | |
<body> | |
<?php | |
$guestbook = array(); | |
if ( file_exists("submissions.json") ) { | |
$file = file_get_contents( "submissions.json" ); | |
$read = json_decode( $file ); | |
$guestbook = $read->data; | |
} | |
// echo "<pre>"; var_dump( $guestbook ); echo "</pre>"; | |
$message = array(); ?> | |
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
if ($_POST["entryindex"] || $_POST["entryindex"] === "0") { | |
$i = intval($_POST["entryindex"]); | |
array_splice($guestbook, $i, 1); | |
$json = new stdClass(); | |
$json->data = $guestbook; | |
// echo "<pre>"; var_dump(json_encode($json)); echo "</pre>"; | |
file_put_contents( "submissions.json", json_encode($json) ); | |
array_push( $message, "Deleted " . $i ); | |
} | |
if ($_POST["name"] && $_POST["comments"]) { | |
$entry = array( | |
"name" => $_POST["name"], | |
"comments" => $_POST["comments"] | |
); | |
array_unshift( $guestbook, (object) $entry ); | |
$json = new stdClass(); | |
$json->data = $guestbook; | |
// echo "<pre>"; var_dump(json_encode($json)); echo "</pre>"; | |
file_put_contents( "submissions.json", json_encode($json) ); | |
array_push( $message, "Thanks for your submission!" ); | |
} | |
} ?> | |
<?php if (!empty($message)) : ?> | |
<div class="message"> | |
<?php foreach ($message as $m) : ?> | |
<p><?php echo $m; ?></p> | |
<?php endforeach; ?> | |
</div> | |
<?php endif; ?> | |
<form action="/" method="POST"> | |
<div class="formgroup"> | |
<label for="name">Name</label> | |
<input type="text" name="name" id="name"> | |
</div> | |
<div class="formgroup"> | |
<label for="comments">Comments</label> | |
<textarea name="comments" id="comments"></textarea> | |
</div> | |
<button type="submit">Submit</button> | |
</form> | |
<?php if ( !empty($guestbook) ) : ?> | |
<div class="submissions"> | |
<?php foreach ( $guestbook as $key => $sub ) : ?> | |
<div class="sub"> | |
<h2><?php echo $sub->name; ?></h2> | |
<p><?php echo $sub->comments; ?></p> | |
<form action="/" method="POST"> | |
<input type="hidden" name="entryindex" value="<?php echo $key; ?>"> | |
<button type="submit">Delete</button> | |
</form> | |
</div> | |
<?php endforeach; ?> | |
</div> | |
<?php else: ?> | |
<p>No submissions.</p> | |
<?php endif; ?> | |
</body> | |
</html> |