简体   繁体   English

如何使复选框显示在联系表单中

[英]How to make checkboxes show up in contact form

I'm fairly new to coding in PHP. 我对使用PHP编码是相当陌生的。 Currently I am having a problem with my checkboxes and dropdown boxes submitting in my contact form. 目前,我在联系表单中提交的复选框和下拉框存在问题。
I am using Bootstrap to build the site. 我正在使用Bootstrap构建站点。
I've researched answers and I have not been able to find why my checkboxes won't show up in my email. 我已经研究了答案,但无法找到为什么我的复选框无法显示在电子邮件中的原因。

Below is my code: 下面是我的代码:

Here is the PHP: 这是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>';
        }
    }
}

Here is the Contact Form: 这是联系表格:

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

If you want them to display in the message, put the information in the value. 如果希望它们显示在消息中,请将信息放入值中。 I would also make the name an array so that you can use foreach to loop through all the check boxes to include them in the email. 我还将名称命名为数组,以便您可以使用foreach遍历所有复选框以将其包括在电子邮件中。

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

You can't use this 你不能用这个

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

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

If you want to retrieve the value, you should use the name of the form element. 如果要检索该值,则应使用form元素的名称。 In this case you're getting nothing because PHP won't recognize those array keys. 在这种情况下,您将一无所获,因为PHP无法识别这些数组键。

$area = $_POST['area'];

It might be 1 or 2 or 3 or 4 , etc, but I noticed in your HTML code, that you don't have a value for the rest of the checkboxes for all the areas, fixing that will help. 可能是1234 ,等等,但是我在您的HTML代码中注意到,对于所有区域,其余复选框都没有值,这一问题将得到解决。

Same aplies to the line 同样适用于生产线

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

You just retrieve the value and do what you want with it but using this instead: $contacting = $_POST['contacting']; 您只需要检索值并执行所需操作,而是使用它:$ contacting = $ _POST ['contacting'];

HERE IS A TIP: 提示:

If you're not using more advanced data processing, you can simply leave the value of the option elements empty and the $_POST['contacting']; 如果您不使用更高级的数据处理,则可以简单地将option元素的值保留为空,并使用$ _POST ['contacting'];。 variable will return the String inside tag. 变量将返回String inside标签。

For example: 例如:

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

Whatever I choose will be send as one of those strings to the PHP instead of the value. 我选择的任何内容都将作为这些字符串之一发送给PHP,而不是值。 It will be easier to concatenate them to your email body. 将它们连接到您的电子邮件正文将更加容易。

With the Checkboxes, you will need the value, indeed, just instead of numbers, use strings to make it easier again, to concatenate to your email body. 使用复选框,实际上,您将需要该值,而不是数字,而是使用字符串使其更容易地连接到您的电子邮件正文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM