【问题标题】:PHP Form with Checkboxes and Dropdown problems when submitting to email提交到电子邮件时带有复选框和下拉列表的 PHP 表单
【发布时间】:2012-09-06 15:55:54
【问题描述】:

好吧,我对 php 或表单不太擅长,但我做了很多挖掘工作,但没有找到我能弄清楚如何工作的解决方案。我有一个带有两组复选框和一个下拉列表的表单,其中包括标准名称、电子邮件等......字段。我的问题是让复选框的值显示在提交表单时收到的电子邮件中。这是表单代码:

    <form method="post" action="appointment-process.php">
<fieldset>
    <label for="name">Name<em class="warning">*</em></label>
    <input id="name" name="name" type="text" class="span6" autofocus required placeholder="Raider Red">
    <span class="help-block">Please enter your FIRST and LAST name.</span>

    <label for="email">Email Address<em class="warning">*</em></label>
    <input id="email" name="email" type="email" class="span6" placeholder="raider.red@ttu.edu" required>

    <label for="phone">Phone Number<em class="warning">*</em></label>
    <input id="phone" name="phone" type="tel" class="span6" placeholder="8067421234" required>

    <label for="legal">Legal Issues<em class="warning">*</em></label>
    <input id="legal" name="legal" type="text" class="span6" placeholder="Landlord Problems" required>  

    <label for="classification">Classification</label>
    <div class="controls">                              
        <select id="classification" name="classification">
            <option>Freshman</option>
            <option>Sophomore</option>
            <option>Junior</option>
            <option>Senior</option>
            <option>Other</option>
        </select>                                       
    </div>
    <div class="control-group">
        <label for="">Preferred Appointment Times</label>
        <span class="help-block">Please check all of the days and times that work with your schedule</span>
        <div class="controls row">
                <div class="span3">
                    <label class="checkbox" for="timez">
                        <input type="checkbox" name="timez[]" value="9am">9:00 am 
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="timez[]" value="10am">10:00 am
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="timez[]" value="11am">11:00 am
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="timez[]" value="1:30pm">1:30 pm
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="timez[]" value="2:30pm">2:30 pm
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="timez[]" value="3:30pm">3:30 pm
                    </label>
                </div>                                              
                <div class="span3">
                    <label class="checkbox" for="dayz">
                        <input type="checkbox" name="dayz[]" value="Monday">Monday
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="dayz[]" value="Tuesday">Tuesday
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="dayz[]" value="Wednesday">Wednesday
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="dayz[]" value="Thursday">Thursday
                    </label>
                </div>

        </div>
    </div>
    <button type="submit" class="btn">Submit</button>
</fieldset> 
</form>

这是我的 php 代码:

<?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
    $name = $_POST['name'] ;
    $email = $_POST['email'] ;
    $phone = $_POST['phone'] ;
    $legal = $_POST['legal'] ;
    $timez = $_POST['timez'] ;
    //$dayz = $_POST['dayz'];

    //form validation to go here....

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

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

    //validate phone number is not empty
    if(empty($phone)){
        $formok = false;
        $errors[] = "You have not entered a phone number";
    }

    //validate legal issue is not empty
    if(empty($legal)){
        $formok = false;
        $errors[] = "You have not entered a legal issue";
    }

    if(isset($_POST['dayz'])){
        $message .= implode(', ', $_POST['dayz']);
    }

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

        $emailbody = "<p>You have recieved a new Appointment Request.</p>
                      <p><strong>Name: </strong> {$name} </p>
                      <p><strong>Email Address: </strong> {$email} </p>
                      <p><strong>Phone Number: </strong> {$phone} </p>
                      <p><strong>Legal Issue: </strong> {$legal} </p>
                      <p><strong>Classification: </strong> {} </p>
                      <p><strong>Preferred Appointment Day(s): </strong> {$dayz} </p>
                      <p><strong>Preferred APpointment Time(s): </strong> {$timez} </p>                   

                      <p>This message was sent on {$date} at {$time}</p>";

        imap_mail("email@email.com","Title",$emailbody,$headers);
    }

    //what we need to return back to our form
    $returndata = array(
        'posted_form_data' => array(
            'name' => $name,
            'email' => $email,
            'timez' => $timez,
            'dayz' => $dayz
        ),
        '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']);

    }


}

?>

任何帮助都将不胜感激。这是我第一次在这里发帖,所以如果我没有遵循正确的协议,请告诉我,我会纠正的。谢谢!

【问题讨论】:

  • 有什么问题?复选框根本不显示吗?出现了错误的?
  • 复选框的值根本没有显示在发送的电子邮件中。
  • 如果您执行var_dump($_REQUEST) 会发生什么 - 值会显示在那里吗?
  • 对不起,andrewsi,但我完全不知道我需要把它放在代码中的什么地方才能尝试一下。不想问,但你能帮我“拼出来”吗?

标签: php forms form-submit checkbox


【解决方案1】:

我猜这行:

<p><strong>Preferred APpointment Time(s): </strong> {$timez} </p>   

只是返回数组。

你需要遍历这个数组来得到选中的:

$timeAv = array();

foreach($timez as $time) {

    if(!empty($time)) {

        $timeAv[] = $time;

    }
}

$timeStr = implode(', ', $timeAv);

这应该建立一个检查时间的数组,然后将它们添加到一个字符串中。我希望我理解正确 - 否则就是浪费了几行代码!

谢谢

【讨论】:

  • 无论出于何种原因,该行根本没有出现任何内容。不过,我会尝试一下,看看会发生什么。非常感谢您的帮助!
  • 我绝对可能做得不对,但添加了上面的代码,现在我得到“解析错误:语法错误,意外”指向if (!empty($time) { 行。 MazB,如果您不介意的话,添加代码的合适位置在哪里?
  • 该行有错别字 - 在$time) 之后缺少右括号;我会修改的。
  • 它现在正在提交,但在提交时我收到以下错误并且仍然没有在电子邮件中列出。 Invalid argument supplied for foreach() on line 21 session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at ...php:21) in ...php on line 105 Cannot modify header information - headers already sent by (output started at ...php:21) in ...php on line 109
  • 检查您的脚本在 标签之前或之后是否有空格 - 并检查您是否没有任何不想要的内容被回显。抱歉打错了-很好的@andrewsi 修复了这个问题:)
猜你喜欢
  • 2011-08-01
  • 2013-09-16
  • 1970-01-01
  • 2013-11-05
  • 2015-11-09
  • 1970-01-01
  • 2018-11-05
  • 1970-01-01
  • 2012-03-01
相关资源
最近更新 更多