HTML5 PHP表单不会发送

时间:2012-06-14 11:11:06

标签: php forms html5

我已经修改了以前的表单构建的新字段,运行正常。客户端验证工作正常但它们似乎是一个php问题,因为电子邮件没有发送。

我还在学习PHP和表格,所以任何帮助都是王牌!

请从页面和下面的php中获取相关代码;

页面

    <div id="contact-form" class="clearfix">
        <h1>SWPEE certificate</h1><br>
        <?php
        //init variables
        $cf = array();
        $sr = false;

        if(isset($_SESSION['cf_returndata'])){
            $cf = $_SESSION['cf_returndata'];
            $sr = true;
        }
        ?>
        <ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">
            <li id="info">There were some problems with your form submission:</li>
            <?php 
            if(isset($cf['errors']) && count($cf['errors']) > 0) :
                foreach($cf['errors'] as $error) :
            ?>
            <li><?php echo $error ?></li>
            <?php
                endforeach;
            endif;
            ?>
        </ul>
        <p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks you. Your information has been submitted.</p>
        <form method="post" action="process2.php">
            <label for="firstname">First Name: <span class="required">*</span></label>
            <input type="text" id="firstname" name="first name" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['first name'] : '' ?>" placeholder="First Name" required autofocus />

            <label for="surname">Surname: <span class="required">*</span></label>
            <input type="text" id="surname" name="surname" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['surname'] : '' ?>" placeholder="Surname" required autofocus />

            <label for="jobtitle">Job title: <span class="required">*</span></label>
            <input type="text" id="jobtitle" name="job title" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['job title'] : '' ?>" placeholder="Job title" required autofocus />

            <label for="office">Office location:</label>
            <select id="office" name="office">
                <option value="Birmingham" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['office'] == 'Birmingham') ? "selected='selected'" : '' ?>>Birmingham</option>
                <option value="Chippenham" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['office'] == 'Chippenham') ? "selected='selected'" : '' ?>>Chippenham</option>
                <option value="Croyden" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['office'] == 'Croyden') ? "selected='selected'" : '' ?>>Croyden</option>
                <option value="Glasgow" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['office'] == 'Glasgow') ? "selected='selected'" : '' ?>>Glasgow</option>
                <option value="Hornsey" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['office'] == 'Hornsey') ? "selected='selected'" : '' ?>>Hornsey</option>
                <option value="London" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['office'] == 'London') ? "selected='selected'" : '' ?>>London</option>
                <option value="Swanley" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['office'] == 'Swanley') ? "selected='selected'" : '' ?>>Swanley</option>
                <option value="York" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['office'] == 'York') ? "selected='selected'" : '' ?>>York</option>
            </select>

            <label for="emp_con">Employee or Contractor:</label>
            <select id="emp_con" name="emp_con">
            <option value="Employee" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['cmp_con'] == 'Employee') ? "selected='selected'" : '' ?>>Employee</option>
                <option value="Contractor" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['cmp_con'] == 'Contractor') ? "selected='selected'" : '' ?>>Contractor</option>
            </select>

            <label for="course_module">Course or Module: <span class="required">*</span></label>
            <input type="text" id="course_module" name="course module" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['course module'] : '' ?>" placeholder="Course or Module name" required autofocus />

            <label for="date">Date Course or Module was completed: <span class="required">*</span></label>
            <input type="text" id="date_completed" name="date completed" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['date completed'] : '' ?>" placeholder="23/7/2012" required autofocus />

            <label for="line_manager">Line Manager: <span class="required">*</span></label>
            <input type="text" id="line_manager" name="line manager" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['line manager'] : '' ?>" placeholder="Line Manager" required autofocus />

            <span id="loading"></span>
            <input type="submit" value="Submit" id="submit-button" />
            <p id="req-field-desc"><span class="required">*</span> indicates a required field</p>
        </form>
        <?php unset($_SESSION['cf_returndata']); ?>
    </div>

处理器(process2.php)我在这里有一个虚拟的电子邮件地址,这不仅仅是一个错字!

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

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

//form data
$first_name = $_POST['first_name'];
$surname = $_POST['surname'];
$job_title = $_POST['job_title'];
$office = $_POST['office'];
$emp_con = $_POST['emp_con'];
$course_module = $_POST['course_module'];
$date_completed = $_POST['date_completed'];
$line_manager = $_POST['line_manager'];

//validate form data

//validate name is not empty
if(empty($first_name)){
    $formok = false;
    $errors[] = "You have not entered your first name";
}

if(empty($surename)){
    $formok = false;
    $errors[] = "You have not entered your surname";
}

if(empty($job_title)){
    $formok = false;
    $errors[] = "You have not entered a job title";
}

if(empty($course_module)){
    $formok = false;
    $errors[] = "You have not entered a course module";
}

if(empty($line_manager)){
    $formok = false;
    $errors[] = "You have not entered the name of your line manager";
}

if(empty($date_completed)){
    $formok = false;
    $errors[] = "You have not entered the date you completed your module";
}

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

    $emailbody = "<p>You have received a new message from the enquiries form on your website.</p>
                  <p><strong>First Name: </strong> {$first_name} </p>
                  <p><strong>Surname: </strong> {$surname} </p>
                  <p><strong>Job title: </strong> {$job_title} </p>
                  <p><strong>Office: </strong> {$office} </p>
                  <p><strong>Employee or Contractor: </strong> {$emp_con} </p>
                  <p><strong>Course or Module title: </strong> {$course_module} </p>
                  <p><strong>Date Course or Module was completed: </strong> {$date_completed} </p>
                  <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

    mail("myemail@address.com","SWPEE Certificate form",$emailbody,$headers);

}

//what we need to return back to our form
$returndata = array(
    'posted_form_data' => array(
        'first_name' => $first_name,
        'surname' => $surname,
        'job_title' => $job_title,
        'office' => $office,
        'emp_con' => $emp_con,
        'course_module' => $course_module,
        'date_completed' => $date_completed,
        'line_manager' => $line_manager
    ),
    '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 个答案:

答案 0 :(得分:0)

if(empty($surename)){
    $formok = false;
    $errors[] = "You have not entered your surname";
}

尝试将$ surename更改为$ surname

if(empty($surname)){
    $formok = false;
    $errors[] = "You have not entered your surname";
}

编辑:哇没看到多久以前这个被发布抱歉只是与我的问题有关所以快速查看并发现错误。