-
Notifications
You must be signed in to change notification settings - Fork 51
/
addressbook.php
75 lines (65 loc) · 1.52 KB
/
addressbook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
if (isset($_REQUEST['action']))
{
$action = $_REQUEST['action'];
if ($action == "delete")
{
$address = $_REQUEST['address'];
$addressbook = file("addressbook.csv");
$f = fopen("addressbook.csv", "w");
foreach ($addressbook as $line)
{
if (strpos($line, $address) !== 0)
fputs($f, $line);
}
fclose($f);
}
if ($action == "add")
{
$line = $_REQUEST['address'].";".$_REQUEST['name']."\n";
$f = fopen("addressbook.csv", "a");
fputs($f, $line);
fclose($f);
}
}
include ("header.php");
echo "<div class='content'>
<h2>Addressbook</h2>";
$addressbook = file("addressbook.csv");
?>
<form method="post">
<input type="hidden" name="action" value="add" />
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="form-control" type="text" name="name" /></td>
<td><input class="form-control" type="text" name="address" /></td>
<td><input class="btn btn-default" type="submit" value="Add new address" /></td>
</tr>
<?php
foreach ($addressbook as $line)
{
$values = explode(";", $line);
$address = $values[0];
$name = str_replace("\n", "", $values[1]);
echo "<tr>";
echo "<td>".$name."</td>";
echo "<td>".$address."</td>";
echo "<td><a href=\"addressbook.php?action=delete&address=".$address."\">Delete</a></td>";
echo "</tr>";
}
?>
</tbody>
</table>
</form>
<?php
echo "</div>";
include ("footer.php");
?>