如果复选框是检查运行Mailchimp代码

时间:2014-03-28 02:34:58

标签: javascript php jquery forms checkbox

我有一个PHP联系表单,其中有一个复选框,如果用户想要订阅时事通讯,用户在注册时可以选择,但我想使用Mailchimp来捕获电子邮件地址并放入列表中。表单提交后我也在使用javascript。 如果检查复选框,我的表单中的代码和mailchimp将同时执行,我该怎么办?

这是我的表格

<form role="form" id="feedbackForm">
      <div class="form-group">
        <input type="text" class="form-control" id="first_name" name="first_name" placeholder="First Name">
        <span class="help-block" style="display: none;">Please enter your name.</span>
      </div>
      <div class="form-group">
        <input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last Name">
        <span class="help-block" style="display: none;">Please enter your name.</span>
      </div>
      <div class="form-group">
        <input type="email" class="form-control" id="email" name="email" placeholder="Email Address">
        <span class="help-block" style="display: none;">Please enter a valid e-mail address.</span>
      </div>
      <div class="form-group">
        <input type="text" class="form-control" id="company_name" name="company_name" placeholder="Company">
        <span class="help-block" style="display: none;">Please enter your name.</span>
      </div>
      <div class="form-group">
        <textarea rows="10" cols="100" class="form-control" id="message" name="message" placeholder="Message"></textarea>
        <span class="help-block" style="display: none;">Please enter a message.</span>
      </div>
      <div class="form-group">
        <label for="selectbasic">How did you hear about us?</label>
            <select id="selectbasic" name="selectbasic" class="form-control">
                <option>Select</option>
                <option>Search engine</option>
                <option>Microsoft DPE</option>
                <option>Microsoft event</option>
                <option>Social media</option>
                <option>Word of mouth</option>
                <option>Other</option>
            </select>
        </div>
      <img id="captcha" src="library/vender/securimage/securimage_show.php" alt="CAPTCHA Image" />
      <a href="#" onclick="document.getElementById('captcha').src = 'library/vender/securimage/securimage_show.php?' + Math.random(); return false" class="btn btn-info btn-sm">Show a Different Image</a><br/>
      <div class="form-group" style="margin-top: 10px;">
        <input type="text" class="form-control" name="captcha_code" id="captcha_code" placeholder="For security, please enter the code displayed in the box." />
        <span class="help-block" style="display: none;">Please enter the code displayed within the image.</span>
      </div>

      <div class="checkbox">
        <label>
           <input type="checkbox" id="emailUpdates" name="emailUpdates" value="Yes">  
            Please keep me informed of product updates and news
        </label>
      </div>

      <span class="help-block" style="display: none;">Please enter a the security code.</span>
      <button type="submit" id="feedbackSubmit" class="btn btn-primary btn-lg" style="display: block; margin-top: 10px;">Send Feedback</button>
    </form>

CHECKBOX的代码 -

      <div class="checkbox">
        <label>
           <input type="checkbox" id="emailUpdates" name="emailUpdates" value="Yes">  
            Please keep me informed of product updates and news
        </label>
      </div>

复选框的PHP脚本

<?php
   if ($_POST['emailUpdates'] == 'Yes') {
     //EXECUTE MAILCHIMP CODE
   }
    ?>

1 个答案:

答案 0 :(得分:0)

只需将复选框的value属性留空,如果已选中,则会提交on值,否则将不提交任何内容

  <?php
  if (isset($_POST['emailUpdates'])) {
    //EXECUTE MAILCHIMP CODE
  }
  ?>


  <input type="checkbox" id="emailUpdates" name="emailUpdates"/>