如何使复选框以联系方式显示

时间:2015-09-23 15:01:22

标签: javascript php html5 twitter-bootstrap twitter-bootstrap-3

我对PHP编码很新。目前我的联系方式和下拉框在我的联系表格中提交时遇到问题 我正在使用Bootstrap来构建网站 我已经研究了答案,但我无法找到为什么我的复选框不会显示在我的电子邮件中。

以下是我的代码:

这是PHP:

if (isset($_POST["submit"])) {
    $branch = $_POST['Toledo Branch, Columbus Branch'];
    $name = $_POST['name'];
    $companyname = $_POST['companyname'];
    $title = $_POST['title'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $contacting = $_POST['Services/Solutions, Quote Request, Service Request, Other '];
    $area = $_POST['area-0, area-1, area-2, area-3, area-4, area-5 '];
    $comment = $_POST['comment'];
    $from = 'ProVideo Website'; 
    $to = 'samantha@provideosystems.com'; 
    $subject = 'Message from ProVideo Contact Form ';

    $body = "Branch: $branch \ 
            From: $name \ 
            Company: $companyname \ 
            Title: $title \ 
            E-Mail: $email \ 
            Phone Number: $phone\ 
            Contacting About: $contacting \ 
            Interest: $area \
            Comment: $comment";

    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    // If there are no errors, send the email
    if (!$errBranch && !$errName && !$errCompanyname && !$errTitle && !$errEmail && !$errPhone && !$errArea && !$errComment ) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You! We have received your message </div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }
}

以下是联系表格:

<h3>Contact Us!</h3>

<div class="form-group">
  <label class="col-md-4 control-label" for="branch">Choose a Branch to Contact</label>
  <div class="col-md-5">
    <select id="contacting" name="contacting" class="form-control">
      <option value="1">Toledo Branch</option>
      <option value="2">Columbus Branch</option>


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

<!-- Text input-->

<div class="form-group">

  <label class="col-md-4 control-label" for="name">Name</label>  

  <div class="col-md-5">
  <input id="name" name="name" type="text" placeholder="First, Last" class="form-control input-md" required=""/>
    <?php echo "<p class='text-danger'>$errName</p>";?>
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="companyname">Company Name</label>  
  <div class="col-md-5">
  <input id="companyname" name="companyname" type="text" placeholder="" class="form-control input-md" required=""/>

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="title">Title</label>  
  <div class="col-md-5">
  <input id="title" name="title" type="text" placeholder="" class="form-control input-md"/>

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="email">Email</label>  
  <div class="col-md-5">
  <input id="email" name="email" type="text" placeholder="name@email.com" class="form-control input-md" required=""/>

  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="phone">Phone </label>  
  <div class="col-md-5">
  <input id="phone" name="phone" type="text" placeholder="(XXX) XXX-XXXX" class="form-control input-md" required=""/>

  </div>
</div>

<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="contacting">I am contacting about...</label>
  <div class="col-md-5">
    <select id="contacting" name="contacting" class="form-control">
      <option value="1">Services/Solutions</option>
      <option value="2">Quote Request</option>
      <option value="">Service Request</option>
       <option value="">Other</option>
    </select>
  </div>
</div>

<!-- Multiple Checkboxes -->
<div class="form-group">
  <label class="col-md-4 control-label" for="area">Area of Interest</label>
  <div class="col-md-4">
  <div class="checkbox">
    <label for="area-0">
      <input type="checkbox" name="area" id="area-0" value="1"/>
      Corporate
    </label>
    </div>
  <div class="checkbox">
    <label for="area-1">
      <input type="checkbox" name="area" id="area-1" value="2"/>
      Education
    </label>
    </div>
  <div class="checkbox">
    <label for="area-2">
      <input type="checkbox" name="area" id="area-2" value=""/>
      House of Worship
    </label>
    </div>
  <div class="checkbox">
    <label for="area-3">
      <input type="checkbox" name="area" id="area-3" value=""/>
      Government
    </label>
    </div>
  <div class="checkbox">
    <label for="area-4">
      <input type="checkbox" name="area" id="area-4" value=""/>
      Sports Venue
    </label>
    <div class="checkbox">
    <label for="area-5">
      <input type="checkbox" name="area" id="area-4" value=""/>
      Other
    </label>
    </div>
    </div>
  </div>
</div>

<!-- Textarea -->
<div class="form-group">
  <label class="col-md-4 control-label" for="comment">Comments</label>
  <div class="col-md-4">                     
    <textarea class="form-control" id="comment" name="comment"></textarea>
  </div>
</div>

<!-- Button -->
<div class="form-group">
  <label class="col-md-4 control-label" for="submit"></label>
  <div class="col-md-4">
<button id="submit" name="submit" class="btn btn-primary">Submit</button>
    </div>
</div>
 <div class="form-group">
        <div class="col-sm-10">

        </div>
    </div>

</form>
</div>

2 个答案:

答案 0 :(得分:1)

如果要在消息中显示它们,请将信息放入值中。我还将该名称设为一个数组,以便您可以使用foreach遍历所有复选框以将其包含在电子邮件中。

 <div class="checkbox">
<label for="area-4">
  <input type="checkbox" name="area[]" id="area-4" value="Sports Venue"/>
  Sports Venue
</label>

答案 1 :(得分:1)

您无法使用此

$contacting = $_POST['Services/Solutions, Quote Request, Service Request, Other '];

$area = $_POST['area-0, area-1, area-2, area-3, area-4, area-5 '];

如果要检索值,则应使用表单元素的名称。在这种情况下,你不会得到任何东西,因为PHP不会识别那些数组键。

$area = $_POST['area'];

可能 1 2 3 4 等,但我注意到您的HTML代码,您没有为所有区域的其余复选框设置值,修复这将有所帮助。

同样适用于该行

$_POST['Services/Solutions, Quote Request, Service Request, Other '];

您只需检索该值并使用它执行您想要的操作,但请使用此代码:    $ contact = $ _POST [&#39;联系&#39;];

这是一个提示:

如果您没有使用更高级的数据处理,则只需将选项元素的值留空即可将$ _POST [&#39;联系&#39;];变量将返回String内部标记。

例如:

<select name="contacting">
    <option>This option</option>
    <option>Another option</option>
    <option>Tacos y Quesadillas</option>
</select>

无论我选择什么,都将作为其中一个字符串发送给PHP而不是值。将它们连接到您的电子邮件正文会更容易。

使用复选框,您确实需要值,而不是数字,使用字符串使其更容易再次连接,以便连接到您的电子邮件正文。