You are given HTML and
JS files for an address book web
application. The javascript file sends requests to a web service that reads and
saves address data. You can view the working application
here.
Write the PHP file, addressbook.php
, that provides the following behavior:
GET
request is sent to addressbook.php
, then print
out, in plain text, a comma separated list of the names of people in the address
book. If the name
parameter is set, then print
out the address associated with that name.
POST
request is sent to addressbook.php
, then you
must add a name/address pair to the address book's data. You may assume that the
user is passing a name
and address
parameter. You will
need to save the data in the address book in some way. You may choose the specific
method.
Extra: Edit addressbook.js
to add Scriptaculous effects to the page.
header("Content-type: text/plain"); $file_text = file_get_contents("addresses.txt"); if ($_SERVER['REQUEST_METHOD'] == "GET") { $lines = explode("\n", $file_text); if (isset($_GET["name"])) { $name = $_REQUEST["name"]; foreach ($lines as $line) { $info = explode(",", $line); if ($info[0] == $name) { print $info[1]; } } } else { $names = array(); foreach ($lines as $line) { $info = explode(",", $line); array_push($names, $info[0]); } print(implode($names, ",")); } }
if ($_SERVER['REQUEST_METHOD'] == "POST") { $name = $_REQUEST["name"]; $address = $_REQUEST["address"]; $file_text = file_get_contents("addresses.txt"); $file_text .= "\n" . $name . "," . $address; file_put_contents("addresses.txt", $file_text); }