无法使用复选框PHP发送表单

时间:2017-02-04 17:20:46

标签: php html forms checkbox

我想发送一个包含复选框的表单,它工作正常,但我不是我现在所做的,我每次发送时都会收到错误。我认为问题与复选框有关,因为我记得我过去遇到过问题。我正在使用PHP中的单个文件。

这是表格的代码:

<div class="contactFrm">
 <?php if(!empty($statusMsg)){ ?>
            <p class="statusMsg <?php echo !empty($msgClass)?$msgClass:''; ?>"><?php echo $statusMsg; ?></p>
        <?php } ?>
        <form action="" method="post">
        <h6>Contact Information</h6>
            <fieldset id="left">
            <h4>First Name</h4>
            <input type="text" name="name" required>
            <h4>Company Name</h4>
            <input type="text" name="compname" required>
            <h4>Phone Number</h4>
            <input type="text" name="phone" required>

            <h4>Date of Arrival</h4>
            <input type="date" name="ardate" required>
            </fieldset>
            <fieldset id="right">
            <h4>Last Name</h4>
            <input type="text" name="lname" required>
            <h4>Website URL</h4>
            <input type="text" name="website" required>
            <h4>Email</h4>
            <input type="email" name="email" required>
            <h4>Date of Departure</h4>
            <input type="date" name="depdate" required>
            </fieldset>
            <h4>What is your estimated time of arrival?</h4>
            <input type="text"  name="estimated_time" required>
             <h4>How many groups will you be hosting?</h4>
            <textarea name="groups" required> </textarea>
            <h4>How many participants per group do you need to accommodate?</h4>
            <input type="text"  name="mparticipants" required>
             <h4>What are the date/time scheduled for each group?</h4>
            <textarea name="scheduled" required> </textarea>
            <h4>What kind of set-up will you like?</h4>
            <p>Additional fees may apply for living room/specialty set-ups.</p>
            <p><div class="inline-field">
               <label>
                <input type="checkbox" name="CheckboxGroup1[]" value="living_room" id="CheckboxGroup1_0">
                Living Room</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup1[]" value="conference_room" id="CheckboxGroup1_1">
                Conference Room</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup1[]" value="other" id="CheckboxGroup1_2">
                Other (please specify at the end of the form)</label>
              <br>
            </div></p>
            <h6>Clients</h6>
            <h4>How many individuals/clients from your company will be attending?</h4>
            <p>These individuals will be stationed in the client room.</p>
            <input type="text"  name="attending">
            <h6>Services</h6>
            <h4>Which technology services are you interested in?</h4>
            <p>Check all that apply (All of these services are charged separately. Some will involve additional costs from the estimate provided to you.)</p>
            <p><div class="inline-field">
              <label>
                <input type="checkbox" name="CheckboxGroup2[]" value="sd_dvd" id="CheckboxGroup2_0">
                Stationary SD DVD Recording</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup2[]" value="hd_video" id="CheckboxGroup2_1">
                Stationary HD Video Recording</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup2[]" value="streaming" id="CheckboxGroup2_2">
                High Definition Video Streaming (Focus Vision (SD) streaming is also available)</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup2[]" value="smartboard" id="CheckboxGroup2_3">
                Smartboard (also functions as TV/DVD/PC)</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup2[]" value="translation" id="CheckboxGroup2_4">
                Translation Equipment</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup2[]" value="videographer" id="CheckboxGroup2_5">
                Videographer Assisted Recording (SD/HD)</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup2[]" value="other" id="CheckboxGroup2_6">
                Other (please specify on notes section)</label>
              <br>
            </div></p>
            <h4>Will you need meal(s) to be provided?</h4>
            <p>You will be able to pick from various menus once the booking is confirmed.</p>
            <p><div class="inline-field">
              <label>
                <input type="checkbox" name="CheckboxGroup3[]" value="participants" id="CheckboxGroup3_0">
                Yes, for Participants</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup3[]" value="clients" id="CheckboxGroup3_1">
                Yes, for Clients</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup3[]" value="breakfast" id="CheckboxGroup3_2">
                Breakfast</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup3[]" value="lunch" id="CheckboxGroup3_3">
                Lunch</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup3[]" value="dinner" id="CheckboxGroup3_4">
                Dinner</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup3[]" value="snacks" id="CheckboxGroup3_5">
                Snacks</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup3[]" value="beverages" id="CheckboxGroup3_6">
                Beverages</label>
              <br>
            </div></p>
            <h4>Do you need research service(s)?</h4>
            <p>Check all that apply.</p>
            <p><div class="inline-field">
              <label>
                <input type="checkbox" name="CheckboxGroup4[]" value="moderator" id="CheckboxGroup4_0">
                Moderator(s)</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup4[]" value="recruiting" id="CheckboxGroup4_1">
                Recruiting</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup4[]" value="reporting" id="CheckboxGroup4_2">
                Reporting and Analysis</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup4[]" value="quantitative" id="CheckboxGroup4_3">
                Quantitative</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup4[]" value="other" id="CheckboxGroup4_4">
                Other Research Services (please specify in the notes section)</label>
              <br>
              <label>
                <input type="checkbox" name="CheckboxGroup4[]" value="na" id="CheckboxGroup4_5">
                N/A</label>
              <br>
            </div></p>
            <h6>Additional Information</h6>
             <h4>Please include any additional information or document that would help us facilitate your request.</h4>
            <textarea name="additionalinfo"> </textarea>
            <h4>Document Upload</h4>
            <input type="file">
            <input type="submit" name="submit" value="Submit">
            <div class="clear"> </div>
        </form>

这是PHP:

 <?php
$statusMsg = '';
$msgClass = '';
if(isset($_POST['submit'])){
// Get the submitted form data
$name = $_POST['name'];
$lname = $_POST['lname'];
$compname = $_POST['compname'];
$phone = $_POST['phone'];
$ardate = $_POST['ardate'];
$website = $_POST['website'];
$email = $_POST['email'];
$depdate = $_POST['depdate'];
$estimated_time = $_POST['estimated_time'];
$groups = $_POST['groups'];
$mparticipants = $_POST['mparticipants'];
$scheduled = $_POST['scheduled'];
$additionalinfo = $_POST['additionalinfo'];
$attending = $_POST['attending'];

$CheckboxGroup1 = (implode("," , $_POST['CheckboxGroup1']));


$CheckboxGroup2 = (implode("," , $_POST['CheckboxGroup2']));


$CheckboxGroup3 = (implode("," , $_POST['CheckboxGroup3']));


$CheckboxGroup4 = (implode("," , $_POST['CheckboxGroup4']));



// Check whether submitted data is not empty
if(!empty($name) && !empty($lname) && !empty($compname) && !empty($phone) && !empty($ardate) && !empty($website) && !empty($email) && !empty($depdate) && !empty($estimated_time) && !empty($groups) && !empty($mparticipants) && !empty($scheduled) && isset($CheckboxGroup1) && isset($CheckboxGroup2) && isset($CheckboxGroup3) && isset($CheckboxGroup4) && !empty($attending) && !empty($additionalinfo)){

    if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
        $statusMsg = 'Please enter your valid email.';
        $msgClass = 'errordiv';
    }else{
        // Recipient email
        $toEmail = 'osek2112@gmail.com';
        $emailSubject = 'Facility Request Submitted by '.$name;
        $htmlContent = '<h2>Facility Request Form</h2>
            <h4>Name</h4><p>'.$name.'</p>
            <h4>Last Name</h4><p>'.$lname.'</p>
            <h4>Company Name</h4><p>'.$compname.'</p>
            <h4>Phone Number</h4><p>'.$phone.'</p>
            <h4>Date of Arrival</h4><p>'.$ardate.'</p>
            <h4>Website URL</h4><p>'.$website.'</p>
            <h4>Email</h4><p>'.$email.'</p>
            <h4>Date of Departure</h4><p>'.$depdate.'</p>
            <h4>What is your estimated time of arrival?</h4><p>'.$estimated_time.'</p>
            <h4>How many groups will you be hosting?</h4><p>'.$groups.'</p>
            <h4>How many participants per group do you need to accommodate?</h4><p>'.$mparticipants.'</p>
            <h4>What are the date/time scheduled for each group?</h4><p>'.$scheduled.'</p>
            <h4>What kind of set-up will you like?</h4><p>'.$CheckboxGroup1.'</p>
            <h4>Which technology services are you interested in?</h4><p>'.$CheckboxGroup2.'</p>
            <h4>How many individuals/clients from your company will be attending?</h4><p>'.$attending.'</p>
            <h4>Will you need meal(s) to be provided?</h4><p>'.$CheckboxGroup3.'</p>
            <h4>Do you need research service(s)?</h4><p>'.$CheckboxGroup4.'</p>
            <h4>Additional Information</h4><p>'.$additionalinfo.'</p>';


        // Set content-type header for sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

        // Additional headers
        $headers .= 'From: '.$name.'<'.$email.'>'. "\r\n";


        // Send email
        if(mail($toEmail,$emailSubject,$htmlContent,$headers)){
            $statusMsg = 'Your contact request has been submitted successfully !';
            $msgClass = 'succdiv';
        }else{
            $statusMsg = 'Your contact request submission failed, please try again.';
            $msgClass = 'errordiv';
        }
    }
}else{
    $statusMsg = 'Please fill all the fields.';
    $msgClass = 'errordiv';
}
}
?>

任何帮助指出我的错误都将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果您收到此消息:

  

您的联系请求提交失败,请重试。

然后这部分代码抛出了这条消息:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="search" placeholder="Type to search" class="clearable" />
<div id="pool">
  <div class="graph">
    <span>graph</span>
    <div class="parent">
      <span>Personal Info1</span>
      <div>
        <fieldset>value1</fieldset>
        <div class="child">
          <span>Id </span>
          <span>111</span>
          </br>
          <span>name </span>
          <span>Jared</span>
          </br>

        </div>
        <div class="child">
          <span>Id</span>
          <span>222</span>
          </br>
          <span>name</span>
          <span>3rd name</span>
          </br>

          <span class="xtags">fathers name </span>
          <span class="xtags" contenteditable="true">Padelk</span>
        </div>
      </div>
    </div>

    <div name="connections" class="parent">
      <span class="xtags" type="connections">connections</span>
      <div>
        <div name="connection" class="child">
          <div class="child" type="connection"><span>con1</span>	
            <span contenteditable="true">STH</span>
            </br>
          </div>
        </div>
        <div name="connection" class="child"><span class="child">con2</span>
          <div class="child">

            <span contenteditable="true">STH2</span>
            </br>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

这意味着mail()函数返回false。确保mail()函数通过传入您知道有效的参数来工作,例如来自您说的其他联系表单中的参数。此外,也许尝试删除$标头,看看是否有帮助,因为该部分是可选的。

答案 1 :(得分:0)

我已经检查了你的代码并且每次它都在工作但你没有处理它如果没有选中复选框时间发生错误只是处理它, 可能会使用miss it checked boxed selection。 其他明智的是什么类型的错误发生?