【问题标题】:PHP form processing a pre-filled form from a query stringPHP表单处理来自查询字符串的预填充表单
【发布时间】:2013-10-21 15:10:32
【问题描述】:

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

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

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

任何帮助解决这个问题,即使它正在重新考虑我可能会如何去做,将不胜感激。

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

表单页面:

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

【问题讨论】:

  • “表单无法正确处理”。究竟是什么问题?您收到错误消息吗?
  • 表单验证显然失败,并告诉我字段丢失,即使它们已明确填写。如果我注释掉验证并运行脚本,一切正常,但我收到的电子邮件不包含任何数据,仅包含字段名称和 IP 地址、日期和时间。

标签: php forms query-string


【解决方案1】:

几个问题:

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'] 为空。

我还没有检查其余部分,但您应该先尝试解决这些问题。

【讨论】:

  • 摇滚先生。做到了。
  • 我确实注意到,这不是原始问题的一部分,即使有多个字段为空,错误列表中也只会列出第一个空字段。例如,如果我清除姓氏和组织,则会出现错误警报,但它只说我错过了姓氏,没有关于错过组织的信息。这里有什么想法吗?
  • 我想这是因为你所有的测试都是互斥的。你做了一系列else if,所以当第一个失败时,条件是true,并且不执行else。您应该对每个字段进行独立的if{...} 检查。
  • 最后一个问题,我想我可以搁置这个表格。如果用户清除一个字段并尝试提交表单,它将触发警报消息,但由于它重新加载页面,它会从查询字符串重新填充表单。我猜这没问题,因为无论如何它都是预期的结果,但是,如果用户更改字段并提交表单,它将使用查询字符串中的数据而不是更改的信息重新加载。有没有办法在重新加载时将任何更改的数据传递回查询字符串,或者至少在表单中填写所做的更改并在此时忽略查询字符串?
  • 为了确保这个脚本的安全,您还需要做很多事情。但是您可以检查$_SESSION 变量并在它们存在时使用它们而不是$_GET,例如$FirstName = !empty( $_SESSION['cf_returndata']['posted_form_data']['FirstName'] ) ? $_SESSION['cf_returndata']['posted_form_data']['FirstName']:$_GET['FirstName'];
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 2019-12-22
  • 1970-01-01
  • 2013-08-07
  • 2015-11-21
  • 1970-01-01
  • 2012-05-10
  • 1970-01-01
相关资源
最近更新 更多