I have an HTML form file and a PHP file that are supposed to interact with eachother to save information from the form to a contactlist text file. I had it working fine and it has been turned it, but started playing with it and now have a question.
Is it possible to assign more than one form element to one php variable? I want my form to have a specific text field for the area code, the prefix and the line number and then want to combine those three field into one variable. This is not a requirement for my class, I'm just wanting to see how much I can do with this. Here is my html code below:
<html>
<!--Patricia Oswalt - IT250 Unit 4 Assignment-->
<head>
<title>Telephone Directory Input</title>
</head>
<body>
<h1>New Directory Listing</h1>
<form action="SaveDirectory.php" method="post" enctype="application/x-www-form-urlencoded">
<p>Last Name <input type="text" name="LastName" size="30" tabindex="1" />
First Name <input type="text" name="FirstName" size="20" tabindex="2" /></p>
<p>Street Address <input type="text" name="StreetAdd" size="50" tabindex="3" /></p>
<p>City <input type="text" name="City" size="30" tabindex="4" />
State (Abbr) <input type="text" name="State" size="2" tabindex="5" />
Zip Code<input type="text" name="ZipCode" size="5" tabindex="6" /></p>
<p>Area Code (<input type="text" name="AreaCode" size="3" tabindex="7"/>)
Phone Number <input type="text" name="Prefix" size="3" tabindex="8" />-<input type="text" name="LineNumber" size="4" tabindex="9" /></p>
<p><input type="submit" value="Save" tabindex="9" /><input type="reset" tabindex="10" /></p>
</form>
<p><a href="InputTelephone.html">Add New Listing</a></p>
<p><a href="contactlist.txt">Retrieve Contact List</a></p>
</body>
</html>
Here is my PHP code:
<html>
<!--Patricia Oswalt - IT250 Unit 4 Assignment-->
<head>
<title>Save Directory</title>
</head>
<body>
<?php
//initializing the variables
$LastName = ($_POST['LastName']);
$FirstName = ($_POST['FirstName']);
$StreetAdd = ($_POST['StreetAdd']);
$City = ($_POST['City']);
$State = ($_POST['State']);
$ZipCode = ($_POST['ZipCode']);
$AreaCode = ($_POST['AreaCode']);
$PhoneNum = ($_POST['Prefix']['LineNumb']);
//confirming that all variables will contain values
if (empty($LastName) ||
empty($FirstName) ||
empty($StreetAdd) ||
empty($City) ||
empty($State) ||
empty($ZipCode) ||
empty($AreaCode) ||
empty($PhoneNum))
echo "<p>You must completely fill out the form. Please use your browser's Back button to return to the form.</p>";
else
//creating, writing and saving to the text file.
$NewContact = 'contactlist.txt';
$Contact = fopen($NewContact, 'a+');
$output ="n $LastName n";
$output .="n $FirstName n";
$output .="n $StreetAdd n";
$output .="n $City n";
$output .="n $State n";
$output .="n $ZipCode n";
$output .="n $AreaCode n";
$output .="n $PhoneNum['Prefix']['LineNumb'] n";//this is where the error in pointing to.
echo "New Contact Saved Successfully!";
fwrite($Contact, $output);
fclose($Contact);
// Split a string into an index array
$Contacts = "$LastName;$FirstName;$StreetAdd;$City;$State;$ZipCode;$AreaCode;$PhoneNum";
$ContactsArray = explode(";", $Contacts);
echo "<br><strong>Contact List</strong><br><br>";
for($i=0; $i<count($ContactsArray); $i++) {
echo "$ContactsArray[$I]<br>";
}
// Sort an index array
sort($ContactsArray);
echo "<br><strong>Sorted Contact List</strong><br><br>";
foreach($ContactsArray as $Contacts) {
echo "$Contacts<br>";
}
?>
</body>
</html> Software/Hardware used:
CodeLobster PHP version 3.2.2; WAMP server; PHP 5.3.0; Windows XP Pro
ASKED:
March 13, 2010 7:46 PM
UPDATED:
March 15, 2010 11:15 PM
Awesome! Thank you!