0

I'm trying to get a form pre-filled using a query string and let the user make any necessary changes then press submit and have the form process and send an email to an administrator for doing whatever it is they need to do with the information.

I'm using PHP to populate the form and I started with a tutorial from NetTuts for the email form processing because it did validation inline. Hopefully validation isn't necessary because all the fields are pre-filled but I wanted to have the form check just to make sure the user doesn't clear a field before submitting the form. I'm at a loss as to why the form won't process correctly.

The only changes between my form and the tutorial are in variable names, the inclusion and some $_GET superglobals to grab the form data from the query string, and the use of echo to fill out the form from the $_GET superglobals instead of the session data should the user submit the form without filling everything out. Everything else has been copied verbatim from the tutorial.

Any help solving this problem, even if it's rethinking how I might go about doing this, would be much appreciated.

Below is the code for the form page and the processing page.

Form Page:

<?php
session_start();

// site root folder
$root_folder = "/meetingplannersignup";

//get values of displayed form fields from URL
$FirstName= $_GET['FirstName'];
$LastName = $_GET['LastName'];
$Organization = $_GET['Organization'];
$EmailAddress = $_GET['EmailAddress'];
$Phone = $_GET['Phone'];
$EventType = $_GET['EventType'];
$EventName = $_GET['EventName'];
$EventLocation = $_GET['EventLocation'];
$HotelName = $_GET['HotelName'];
$EventStart = $_GET['EventStart'];
$EventEnd = $_GET['EventEnd'];

// get values of hidden form fields from URL
$ExtReferenceID = $_GET['ExtReferenceID'];
$City = $_GET['City'];
$State = $_GET['State'];
$ZipCode = $_GET['ZipCode'];
$CountryCode = $_GET['CountryCode'];
?>
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--><html class="no-js" lang="en" > <!--<![endif]-->

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width">
        <title>Simplify Event Management with GroupMAX</title>

        <link rel="stylesheet" href="<?php echo $root_url ?>/assets/css/bootstrap.css">

    </head>
    <body>
        <div class="container">

<!-- begin main nav -->
            <nav class="navbar navbar-static-top navbar-inverse" role="navigation">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-main-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                </div>

                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse navbar-main-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="<?php echo $root_url ?>">home</a></li>
                        <li><a href="<?php echo $root_url ?>/index.php?FirstName=Todd&LastName=Bailey&Organization=Passkey%20International,%20Inc.&EmailAddress=tbailey@passkey.com&Phone=781-373-4100&EventType=event type&EventName=event%20name&EventLocation=Honolulu,%20Hawaii&HotelName=Marriott%20Resorts&&EventStart=11/20/2013&EventEnd=11/24/2013&ExtReferenceID&City=Waltham&State=MA&ZipCode=02453&CountryCode=US">fill out form</a></li>
                    </ul>
                </div><!-- /.navbar-collapse -->
            </nav>
<!-- end main nav -->

            <div class="row">
                <div class="col-lg-12">
                    <h1>Simplify Event Management with GroupMAX</h1>
                    <h3>Impress Your Attendees. Optimize Your Event.</h3>
                    <hr />
                </div>
            </div>

            <div class="row">
                <div class="col-lg-6">
                    <h4>Included Features are:</h4>
                    <ul>
                        <li><strong>Event Booking Websites – </strong>Passkey’s award winning booking website allows for personalized hotel reservation website where attendees can make, modify or cancel their hotel bookings directly into that group's contracted block.</li>
                        <li><strong>Integrated with Event Registration - </strong>RegLink&trade; is an integration technology that can link any online planner registration solution to Passkey's best-in-class hotel reservation system, allowing meeting planners to integrate hotel reservations directly into their event registration process.</li>
                        <li><strong>Event Dashboard – </strong>With Event Dashboard Planners can track their events, manage their lists and monitor reservations anytime online. With Passkey’s LiveView Dashboards, meeting planners can get an instant snapshot of their event in a fun, interactive environment. </li>
                        <li><strong>SmartAlerts&trade; - </strong>Automatic e-mails containing vital event information that are automatically sent out to a list of recipients at specific intervals or critical event milestones.</li>
                    </ul>
                </div>

                <div class="col-lg-6">
                    <div class="row">
                        <div class="col-lg-12">

<!-- begin error processing -->
                            <div class="well">
                                <?php
                                    //init variables
                                    $cf = array();
                                    $sr = false;

                                    if(isset($_SESSION['cf_returndata'])){
                                        $cf = $_SESSION['cf_returndata'];
                                        $sr = true;
                                    }
                                ?>
                                <div id="errors" class="alert alert-danger<?php echo ($sr && !$cf['form_ok']) ? ' show_alert' : ''; ?>">
                                    <p>There were some problems with your form submission:</p>
                                    <ul>
                                    <?php 
                                        if(isset($cf['errors']) && count($cf['errors']) > 0) :
                                            foreach($cf['errors'] as $error) :
                                    ?>
                                    <li><?php echo $error ?></li>
                                    <?php
                                            endforeach;
                                        endif;
                                    ?>
                                    <?php
                                        //init variables
                                        $cf = array();
                                        $sr = false;

                                        if(isset($_SESSION['cf_returndata'])){
                                            $cf = $_SESSION['cf_returndata'];
                                            $sr = true;
                                        }
                                    ?>
                                    </ul>
                                </div>
                                <p id="success" class="alert alert-success<?php echo ($sr && $cf['form_ok']) ? ' show_alert' : ''; ?>">Thanks for your message! We will get back to you ASAP!</p>
<!-- end error processing -->
<!-- begin form -->
                                <fieldset>
                                    <legend>Your Information</legend>
                                    <p>Please review the pre-filled information and correct any inaccurate information prior to submitting the form.</p>
                                    <form method="post" action="process.php">
                                        <div class="form-group">
                                            <label>First Name</label>
                                            <input type="text" class="form-control" id="FirstName" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['FirstName'] : '' ?><?php echo $FirstName; ?>">
                                        </div>
                                        <div class="form-group">
                                            <label>Last Name</label>
                                            <input type="text" class="form-control" id="LastName" value="<?php echo $LastName; ?>">
                                        </div>
                                        <div class="form-group">
                                            <label>Company/Organization</label>
                                            <input type="text" class="form-control" id="Organization" value="<?php echo $Organization; ?>">
                                        </div>
                                        <div class="form-group">
                                            <label>Email Address</label>
                                            <input type="text" class="form-control" id="EmailAddress" value="<?php echo $EmailAddress; ?>">
                                        </div>
                                        <div class="form-group">
                                            <label>Phone Number</label>
                                            <input type="text" class="form-control" id="Phone" value="<?php echo $Phone; ?>">
                                        </div>
                                        <div class="form-group">
                                            <label>Event Type</label>
                                            <input type="text" class="form-control" id="EventType" value="<?php echo $EventType; ?>">
                                        </div>
                                        <div class="form-group">
                                            <label>Event Name</label>
                                            <input type="text" class="form-control" id="EventName" value="<?php echo $EventName; ?>">
                                        </div>
                                        <div class="form-group">
                                            <label>Event Location</label>
                                            <input type="text" class="form-control" id="EventLocation" value="<?php echo $EventLocation; ?>">
                                        </div>
                                        <div class="form-group">
                                            <label>Hotel Name</label>
                                            <input type="text" class="form-control" id="HotelName" value="<?php echo $HotelName; ?>">
                                        </div>
                                        <div class="form-group">
                                            <label>Start/Arrival Date</label>
                                            <input type="text" class="form-control" id="EventStart" value="<?php echo $EventStart; ?>">
                                        </div>
                                        <div class="form-group">
                                            <label>End Date</label>
                                            <input type="text" class="form-control" id="EventEnd" value="<?php echo $EventEnd; ?>">
                                        </div>
                                        <hr />
                                        <input type="submit" value="Submit" class="btn btn-primary" />

                                        <!--hidden fields-->
                                        <input type="hidden" id="ExtReferenceID" value="<?php echo $ExtReferenceID; ?>">
                                        <input type="hidden" id="City" value="<?php echo $City; ?>">
                                        <input type="hidden" id="State" value="<?php echo $State; ?>">
                                        <input type="hidden" id="ZipCode" value="<?php echo $ZipCode; ?>">
                                        <input type="hidden" id="CountryCode" value="<?php echo $CountryCode; ?>">

                                    </form>
                                    <?php unset($_SESSION['cf_returndata']); ?>
                                </fieldset>
                            </div>
<!-- end form -->

                        </div>
                    </div>
                </div>
            </div>
        </div>

    </body>
</html>

Processing Page:

<?php
if( isset($_POST) ){

    //form validation vars
    $formok = true;
    $errors = array();

    //submission data
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    $date = date('d/m/Y');
    $time = date('H:i:s');

    //form data
    $FirstName = $_POST['FirstName'];
    $LastName = $_POST['LastName'];
    $Organization = $_POST['Organization'];
    $EmailAddress = $_POST['EmailAddress'];
    $Phone = $_POST['Phone'];
    $EventType = $_POST['EventType'];
    $EventName = $_POST['EventName'];
    $EventLocation = $_POST['EventLocation'];
    $HotelName = $_POST['HotelName'];
    $EventStart = $_POST['EventStart'];
    $EventEnd = $_POST['EventEnd'];

    // hidden form fields
    $ExtReferenceID = $_POST['ExtReferenceID'];
    $City = $_POST['City'];
    $State = $_POST['State'];
    $ZipCode = $_POST['ZipCode'];
    $CountryCode = $_POST['CountryCode'];

    //validate form data
    //validate First Name is not empty
    if(empty($FirstName)){
        $formok = false;
        $errors[] = "You have not entered a First Name";

    //validate Last Name is not empty
    } elseif (empty($LastName)){
        $formok = false;
        $errors[] = "You have not entered a Last Name";

    //validate Company/Organization is not empty
    } elseif (empty($Organization)){
        $formok = false;
        $errors[] = "You have not entered a Company or organization";

    //validate email address is not empty
    } elseif (empty($EmailAddress)){
        $formok = false;
        $errors[] = "You have not entered an email address";

    //validate email address is valid
    } elseif (!filter_var($EmailAddress, FILTER_VALIDATE_EMAIL)){
        $formok = false;
        $errors[] = "You have not entered a valid Email Address";

    //validate Last Name is not empty
    } elseif (empty($Phone)){
        $formok = false;
        $errors[] = "You have not entered a Phone Number";

    //validate Last Name is not empty
    } elseif (empty($EventType)){
        $formok = false;
        $errors[] = "You have not entered an Event Type";

    //validate Last Name is not empty
    } elseif (empty($EventName)){
        $formok = false;
        $errors[] = "You have not entered an Event Name";

    //validate Last Name is not empty
    } elseif (empty($EventLocation)){
        $formok = false;
        $errors[] = "You have not entered an Event Location";

    //validate Last Name is not empty
    } elseif (empty($HotelName)){
        $formok = false;
        $errors[] = "You have not entered a Hotel Name";

    //validate Last Name is not empty
    } elseif (empty($EventStart)){
        $formok = false;
        $errors[] = "You have not entered an Event Start Date";

    //validate Last Name is not empty
    } elseif (empty($EventEnd)){
        $formok = false;
        $errors[] = "You have not entered an Event End Date";
    }

    //send email if all is ok
    if($formok){
        $headers = "From: meetingplannersignup@passkey.com" . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

        $emailbody = "<p>You have recieved a new meeting planner signup registration:</p>
            <p><strong>First Name: </strong> {$FirstName}</p>
            <p><strong>Last Name: </strong> {$LastName}</p>
            <p><strong>Company/Organization: </strong> {$Organization}</p>
            <p><strong>Email Address: </strong> {$EmailAddress}</p>
            <p><strong>Phone: </strong> {$Phone}</p>
            <hr />
            <p><strong>Event Type: </strong> {$EventType}</p>
            <p><strong>Event Name: </strong> {$EventName}</p>
            <p><strong>Event Location: </strong> {$EventLocation}</p>
            <p><strong>Hotel Name: </strong> {$HotelName}</p>
            <p><strong>Event Start Date: </strong> {$EventStart}</p>
            <p><strong>Event End Date: </strong> {$EventEnd}</p>
            <hr />
            <p><strong>Reference ID: </strong> {$ExtReferenceID}</p>
            <p><strong>City: </strong> {$City}</p>
            <p><strong>State: </strong> {$State}</p>
            <p><strong>Zip Code: </strong> {$ZipCode}</p>
            <p><strong>Country Code: </strong> {$CountryCode}</p>
            <hr />
            <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>    ";

        mail("stuart@monderer.com","Meeting Planner Signup",$emailbody,$headers);
    }

    //what we need to return back to our form
    $returndata = array(
        'posted_form_data' => array(
            'FirstName' => $FirstName,
            'LastName' => $LastName,
            'EmailAddress' => $EmailAddress,
            'Organization' => $Organization,
            'Phone' => $Phone,
            'EventType' => $EventType,
            'EventName' => $EventName,
            'EventLocation' => $EventLocation,
            'HotelName' => $HotelName,
            'EventStart' => $EventStart,
            'EventEnd' => $EventEnd,
            'ExtReferenceID' => $ExtReferenceID,
            'City' => $City,
            'State' => $State,
            'ZipCode' => $ZipCode,
            'CountryCode' => $CountryCode
        ),
        'form_ok' => $formok,
        'errors' => $errors
    );

    //if this is not an ajax request
    if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
        //set session variables
        session_start();
        $_SESSION['cf_returndata'] = $returndata;

        //redirect back to form
        header('location: ' . $_SERVER['HTTP_REFERER']);
    }
}
4

1 に答える 1