Ok, so here is my problem. I'm making a telephone registry that lets a user add information such as first/last name, address, zipcode, state, and telephone number.
Then they save the contact. Which seems to be where my problem is. The contact is saved and their information is posted. A file is created in the directory. HOWEVER for every contact I make it is making a new file for each one instead of appending it to the same file.
Then when I go to do a search by last name of my contacts it will not retrieve any of my contacts that I have saved. Everything else seems to be working correctly. I just can't get the program to put all my contacts in one file, and retrieve and saved contacts. Maybe someone can see where I'm going wrong, any help is much appreciated.
Here is my code:
AddContact.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Main Directory</title>
<meta http-equiv="content-type"
content="txt/html; charset=iso-8859-1" />
</head>
<body>
<h1>Telephone Directory</h1>
<form action="AddInfo.php" method="post" enctype="application/x-www-form-urlencoded">
<p><b>First Name:</b> <input type="text" name="firstName" size="25" /> <b>Last Name:</b> <input type="text" name="lastName" size="25" /></p>
<p><b>Address:</b> <input type="text" name="address" size="30" /></p>
<p><b>City:</b> <input type="text" name="city" size="25" /> <b>State:</b> <input type="text" name="state" size="2" /> <b>Zipcode:</b> <input type="text" name="zipCode" size="5" /></p>
<p><b>Telephone Number:</b> <input type="text" name="telNum" size="14" /></p>
<p><input type="submit" value="Save Contact" /><input type="reset" value="Reset Form" /></p>
<p><a href="Contacts.html">Return to the Main Directory page</a></p>
</form>
</body>
</html>
SaveContact.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Save Contact</title>
<meta http-equiv="content-type"
content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
$FirstName = addslashes($_POST['firstName']);
$LastName = addslashes($_POST['lastName']);
$Address = addslashes($_POST['address']);
$City = addslashes($_POST['city']);
$State = addslashes($_POST['state']);
$ZipCode = addslashes($_POST['zipCode']);
$TelNumber = addslashes($_POST['telNum']);
if(empty($FirstName) ||
empty($LastName) ||
empty($Address) ||
empty($City) ||
empty($State) ||
empty($ZipCode) ||
empty($TelNumber))
echo "<hr/><p>You must enter a value in each field.
Click your browser's Back button to return to the Directory.</p><hr />";
else if (!is_numeric($ZipCode) ||
!is_numeric($TelNumber))
echo "<p>The Zip Code and Telephone Number must contain numeric values!
Click your browser's Back button to return to the Directory.</p>";
else {
$Contact = $FirstName . "\n";
$Contact .= $LastName . "\n";
$Contact .= $Address . "\n";
$Contact .= $City . "\n";
$Contact .= $State . "\n";
$Contact .= $ZipCode . "\n";
$Contact .= $TelNumber . "\n";
if (!file_exists("Open"))
mkdir("Open");
$ContactFile = fopen("Open\\" . $LastName . ".txt", "w");
if (flock($ContactFile, LOCK_EX)) {
(fwrite($ContactFile, $Contact) > 0) {
$FirstName = stripslashes($_POST['firstName']);
$LastName = stripslashes($_POST['lastName']);
$Address = stripslashes($_POST['address']);
$City = stripslashes($_POST['city']);
$State = stripslashes($_POST['state']);
$ZipCode = stripslashes($_POST['zipCode']);
$TelNumber = stripslashes($_POST['telNum']);
echo "<h1>Contact Saved</h1>";
echo "<b>Name:</b> $FirstName $LastName <br />";
echo "<b>Address:</b> $Address <br />";
echo "<b>City/State:</b> $City, $State <br />";
echo "<b>Zip Code:</b> $ZipCode <br />";
echo "<b>Telephone #:</b> $TelNumber";
flock($ContactFile, LOCK_UN);
fclose($ContactFile);
}
else
echo "<p>The Contact could not be saved!</p>";
}
else
echo "<p>The Contact could not be saved!</p>";
}
?>
<p><a href="Contacts.html">Return to the Main Directory Page</a></p>
</body>
</html>
Contacts.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Contacts</title>
<meta http-equiv="content-type"
content="txt/html; charset=iso-8859-1" />
</head>
<body>
<h1>Contacts</h1><hr />
<form action="AddNew.html" method="get"
enctype="application/x-www-form-urlencoded">
<p><input type="submit" value="Save Contact" /></p>
</form>
<form action="ViewOpenContacts.php" method="get"
enctype="application/x-www-form-urlencoded">
<p><input type="submit" value="View Open Contacts" /></p>
</form>
<form action="ViewContact.php" method="get"
enctype="application/x-www-form-urlencoded">
<p><input type="submit" value=" View Contact Name " />
<input type="text" name="contactname" /></p>
<p>(Enter an existing Contact by Last Name.)</p>
</form>
</body>
</html>
ViewContact.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>View Contact</title>
<meta http-equiv="content-type"
content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
if (empty($_GET['lastName']))
echo "<hr /><p>You must enter an existing contact by last name.
Click your browser's Back button to return to the Contacts page.</p><hr />";
else if (is_readable("Open\\" . $_GET['lastName'] . ".txt")) {
$ContactFields = fopen("Open\\" . $_GET['lastName'] . ".txt", "r");
$FirstName = stripslashes(fgets($ContactFields));
$LastName = stripslashes(fgets($ContactFields));
$Address = stripslashes(fgets($ContactFields));
$City = stripslashes(fgets($ContactFields));
$State = stripslashes(fgets($ContactFields));
$ZipCode = stripslashes(fgets($ContactFields));
$TelNumber = striplashes(fgets($ContactFields));
echo "<h1>View Contact</h1>";
echo "<strong>First Name</strong>: $FirstName<br />";
echo "<strong>Last Name</strong>: $LastName<br />";
echo "<strong>Address</strong>: $Address<br />";
echo "<strong>City</strong>: $City<br />";
echo "<strong>State</strong>: $State<br />";
echo "<strong>Zip Code</strong>: $ZipCode<br />";
echo "<strong>Telephone Number</strong>: $TelNumber";
fclose($ContactFields);
}
else
echo "<p>Could not read the Contact!</p>";
?>
</body>
</html>
ViewOpenContacts.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>View Open Contacts</title>
<meta http-equiv="content-type"
content="txt/html; charset=iso-8859-1" />
</head>
<body>
<?php
$Dir = "open";
if (is_dir($Dir)) {
$DirEntries = scandir($Dir);
foreach ($DirEntries as $Entry) {
if (strpos($Entry, '.txt') !== FALSE)
echo $Entry . "<br />";
}
}
else
echo "<p>The Open contact does not exist! You must first san a contact to create the open directory.</p>";
?>
<p><a href="Contacts.html">Return to the Main Directory Page</a></p>
</body>
</html>
I ran all my code through the WDG validator and come up with no errors. Everything seems to work just fine except bringing up the contacts when I search. Been trying to figure this out for days now to no avail :).