Is it possible to assign more than one form element to one php variable?

115 pts.
Tags:
Arrays
Arrays (programming)
forms
PHP
PHP scripts
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

Answer Wiki

Thanks. We'll let you know when a new response is added.

Yes, you can do this many ways, here is my favorite:

according to your html file, the POST variables will include
$_POST[‘AreaCode’]
$_POST[‘Prefix’]
$_POST[‘LineNum’]

Change your line
“$PhoneNum = ($_POST[‘Prefix’][‘LineNumb’]);”
to:
$PhoneNum=$_POST[‘Prefix’].”-“.$_POST[‘LineNum’];

So that is using the concat function of php (for combining strings).

In this example we turn
503
555
1234

into

$AreaCode=503
$PhoneNum=”555-1234″

If you do not want the hyphen (so you can store as an integer):
$PhoneNum=intval($_POST[‘Prefix’].$_POST[‘LineNum’]);

Happy coding

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • PatOswalt
    Awesome! Thank you!
    115 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following