#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7

    Adding element to Web form


    I need to revise the code below so that it stores not only the guest name but their email address as well. I have the form changed, I just need to know how to add the email address to the array when it writes to the text file. The way it is now it just writes the first letter of the name and thats all.

    PHP Code:
    <!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" xml:lang="en" lang="en">

    <head>
    <title>Guest Book</title>
    <meta http-equiv="Content-Type"
        content="text/html; charset=iso-8859-1"/>
    </head>

    <!--
    Sample Code
    -->

    <body>
    <h1>Guest Book Array Demonstration</h1>
    <?php
    //action is for the hyperlink -- like ?page
    if(isset($_GET['action'])) {
        
    //if the file guests.txt exists and is not blank
        
    if((file_exists("GuestOrganizer/guests.txt")) && (filesize("GuestOrganizer/guests.txt")!=0)) {

            
    //Gets the guests and puts them in an indexed array
            
    $GuestArray file("GuestOrganizer/guests.txt");

            
    //use troubleshooting technique to see if the array contains values
            //echo "<pre>";
                //print_r($GuestArray);
            //echo "</pre>";


            //the switch statment checks to see which of the actions has been clicked and performs those steps
            //NOTHING HAPPENS HERE UNLESS A ACTION HYPERLINK HAS BEEN CLICKED
            
    switch ($_GET['action']) {

            case 
    'Remove_Duplicates'//removes duplicates from the GuestOrganizer
                
    $GuestArray array_unique($GuestArray); //The array_unique() is used to remove duplicate values from an array
                
    $GuestArray array_values($GuestArray);//assigns all the values of the array to $GuestArray -- but no indexese
                
    break;

            case 
    'Sort_Ascending'://sorts GuestArray ascending
                
    sort($GuestArray);//sorts the array by values
                
    break;

            case 
    'Shuffle'://shuffles the guest names
                
    shuffle($GuestArray);//The shuffle() function randomizes the order of the elements
                
    break;
            }
    //end of switch statement


            //the contents of the guests.txt file are in an ndexed array
            
    if (count($GuestArray)>0) {

                
    //
                
    $NewGuests implode($GuestArray);//turns the indexed array into a string so it can be saved to the text file
                //Opens a handle to the guests.txt file in write binary mode
                
    $GuestStore fopen("GuestOrganizer/guests.txt""wb");

                if(
    $GuestStore ===false){
                    echo 
    "There was an error updating the guest file\n";
                }
    //end of inner if

                
    else{
                    
    fwrite($GuestStore$NewGuests);
                    
    fclose($GuestStore);
                }
    //end of inner else

            
    }//end of if (count($GuestArray)>0)
        
    else{
                
    unlink("GuestOrganizer/guests.txt");
            }
    //end of else
        
    }//end of if((file_exists))
    }//end of if(isset($_GET['action']))


    if(isset($_POST['submit'])) {//handling any data submitted from web form
        
    $GuestToAdd stripslashes($_POST['GuestName']['GuestEmail']) . "\n";
        
    $ExistingGuests = array();

        if(
    file_exists("GuestOrganizer/guests.txt") && filesize("GuestOrganizer/guests.txt")>0) {
            
    $ExistingGuests file("GuestOrganizer/guests.txt");
        }
    //end of if file exists

        
    if (in_array($GuestToAdd$ExistingGuests)) {//checks to see if the guest name is already in list
            
    echo "<p>The guest you entered already exists! <br />\n";
            echo 
    "Your Guest was not added to the list.</p>";
        }
    //end of if in_array

        
    else{//adds new guest to guest list
            
            
    $GuestFile fopen("GuestOrganizer/guests.txt""ab");
            
         
            if (
    $GuestFile ===false) {
                
                echo 
    "There was an error saving your message!\n";
            }
    //end of if ($GuestFile ===false)

            
    else{
                
                
    fwrite($GuestFile$GuestToAdd);
                
                
    fclose($GuestFile);
                echo 
    "Your guest has been added to the list\n";
            }
    //end of else fwrite($GuestFile, $GuestToAdd);
        
    }//end of else $GuestFile = fopen("GuestOrganizer/guests.txt", "ab");
    }//end of if(isset($_POST['submit']

        
    if ((!file_exists("GuestOrganizer/guests.txt")) || (filesize("GuestOrganizer/guests.txt")== 0)) {
                echo 
    "<p>There are no guests in the list.</p>\n";
            }
    //end of if ! file_exists
            
    else{
                
    $GuestArray file("GuestOrganizer/guests.txt");
                echo 
    "<table border=\"1\"width=\"100%\" style=\"background-color: lightgray\">\n";

                foreach(
    $GuestArray as $Guest){
                    echo 
    "<tr>\n";
                    echo 
    "<td>" htmlentities($Guest) . "</td>";
                    echo 
    "</tr>\n";
                }
    //end of foreach loop

                
    echo "</table>\n";
    }
    //end of else


    ?>
    <!-- Displaying hyperlinks for the three functions in the switch statment -->
    <p>
    <a href = "GuestOrganizer.php?action=Sort_Ascending">Sort Guest Book</a><br />

    <a href = "GuestOrganizer.php?action=Remove_Duplicates" >Remove Duplicate Entries</a><br />

    <a href = "GuestOrganizer.php?action=Shuffle">Randomize Guest List</a><br />
    </p>

    <!--Web Form for entering song names into the song list  -->
    <form action= "GuestBookArray.php" method = "post">
    <p>Add New Guest Info to List:</p>

    <p>Guest Name: <input type = "text" name = "GuestName"/></p>
    <p>Guest E-mail: <input type = "text" name = "GuestEmail"/></

    <p>
    <input type = "submit" name = "submit" value = "Add Guest to List"/>
    <input type = "reset" name = "reset" value = "Reset Guest Name">
    </p>

    </form>
    </body>
    </html>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    112
    Rep Power
    7
    What is the syntax error in this line:

    PHP Code:
    $GuestToAdd = ($_POST['GuestName'$_POST['GuestEmail']) . "\n"
    Am I missing a bracket or para?
    Last edited by maineman; October 21st, 2012 at 07:51 PM.

IMN logo majestic logo threadwatch logo seochat tools logo