处理查询字符串中预填充表单的 PHP 表单


PHP form processing a pre-filled form from a query string

我正在尝试使用查询字符串预先填写表单,并让用户进行任何必要的更改,然后按提交并让表单处理并向管理员发送电子邮件,以执行他们需要对信息执行的任何操作。

正在使用PHP来填充表单,我从NetTuts的电子邮件表单处理教程开始,因为它进行了内联验证。希望验证不是必需的,因为所有字段都已预先填写,但我想进行表单检查,以确保用户在提交表单之前不会清除字段。我不知道为什么表单无法正确处理。

我的表单和教程之间的唯一变化是变量名称,包含和一些$_GET超全局变量以从查询字符串中获取表单数据,以及使用echo从$_GET超全局变量而不是会话数据中填写表单,如果用户提交表单而不填写所有内容。其他所有内容均已从教程中逐字复制。

任何帮助解决这个问题,即使是重新考虑我如何去做这件事,也将不胜感激。

下面是表单页和处理页的代码。

表单页面:

<?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>

处理页面:

<?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']);
    }
}

几个问题:

1. isset($_POST)永远是真实的

在处理页面中,您需要检查:

if( isset($_POST) )

即使$_POST为空,这也将始终计算为true。您应该检查特定字段以尝试并猜测表单是否已提交

2. 你用id而不是name

通过POST发送的表单值是通过它们的name来标识的,而不是通过它们的id来标识的。在 HTML 中,您可以保留 id 属性,但对于要发布的每个字段,您必须添加一个 name 属性:

<input type="text" class="form-control" name="LastName" id="LastName" value="<?php echo $LastName; ?>">

如果仅使用 id 标识窗体控件,则$_POST['LastName']为空。

我没有检查其余的,但您应该先尝试修复这些要点。