部分表单未提交

时间:2016-06-05 08:49:26

标签: php jquery html forms

我在处理一个小项目时遇到了问题。

我有一个包含9个字段的HTML表单。如果我点击提交,它会发送一封包含所有表单数据的电子邮件。只有在选择相应的单选按钮时才会显示表单的一部分。虽然这种方法很完美(使用JQuery),但该子目录中的输入字段仍然存在。没有提交。其他输入字段工作得很好。这是我的代码:

HTML:

<form name="contactForm" id="contactForm" method="post" action="">
            <fieldset>
                <div class="group">
                    <input name="groupName" type="text" id="groupName" placeholder="Groepsnaam" value=""
                           minLength="2"
                           required/>
                </div>
                <div>
                    <input name="contactName" type="text" id="contactName" placeholder="Naam contactpersoon"
                           value="" minLength="2"
                           required/>
                </div>
                <div>
                    <input name="contactEmail" type="email" id="contactEmail" placeholder="Email contactpersoon"
                           value=""
                           required/>
                </div>
                <div>
                    <input name="contactPhone" type="tel" id="contactPhone"
                           placeholder="Telefoonnummer contactpersoon" value=""
                           required/>
                </div>
                <div>
                    <select name="amountMembers" id="amountMembers " required>
                        <option value="" selected>Aantal groepsleden</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                    </select>
                </div>
                <div>
                    <textarea name="groupMembers" id="groupMembers" placeholder="Groepsleden" rows="10" cols="50"
                              required></textarea>
                </div>

                <div> <!-- If the first rdb is select, show fields. Else hide. -->

                    <input type="radio" onclick="displayForm(this)" name="food" value="1"> Ik wil me inschrijven
                    voor het pasta-buffet<br>
                    <input type="radio" onclick="displayForm(this)" name="food" value="2"> Ik neem niet deel aan het
                    pasta-buffet<br>
                </div>
                <div id="foodform" style="display:none;">

                    <input name="volwassenenPorties" type="text" id="volwassen" placeholder="Aantal volwassenen"
                           value=""
                           required/>


                    <input name="kinderPorties" type="text" id="kind" placeholder="Aantal kinderen (-12j) " value=""
                           required/>
                    <br>
                </div>
                <div>
                    <button class="submitform">Submit</button>
                    <div id="submit-loader">
                        <div class="text-loader">Sending...</div>
                        <div class="s-loader">
                            <div class="bounce1"></div>
                            <div class="bounce2"></div>
                            <div class="bounce3"></div>
                        </div>
                    </div>
                </div>
            </fieldset>
        </form> <!-- Form End -->

JQuery的:

function displayForm(c) {
if (c.value == "1") {
    jQuery('#foodform').fadeIn();
}
if (c.value == "2") {
    jQuery('#foodform').fadeOut();
}
};

PHP:

<?php
$siteOwnersEmail = 'Myemail@website.be';

if($_POST) {
$groupName = trim(stripslashes($_POST['groupName']));
$contactName = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$phone = trim(stripslashes($_POST['contactPhone']));
$amountMembers = trim(stripslashes($_POST['amountMembers']));
$groupMembers = trim(stripslashes($_POST['groupMembers']));
$food = trim(stripslashes($_POST['food']));
$volwassenen = trim(stripslashes($_POST['volwassen']));
$kinderen = trim(stripslashes($_POST['kind']));

// Set Message
$message .= "Groepsnaam: " . $groupName . "<br />";
$message .= "Naam contactpersoon: " . $contactName . "<br />";
$message .= "email: " . $email . "<br />";
$message .= "telefoonnr: " . $phone . "<br />";
$message .= "Aantal groepsleden: " . $amountMembers . "<br />";
$message .= "Namen groepsleden: " . $groupMembers . "<br />";
$message .= "Eten 1=ja, 2=nee: " . $food . "<br />";
$message .= "Volwassenen porties: " . $volwassenen . "<br />";
$message .= "Kinder porties:  " . $kinderen . "<br />";
$message .= "<br /> ----- <br /> Deze email werd verstuurd via het inschrijvingsformulier. <br />";

// Set From: header
$from =  $contactName . " <" . $email . ">";

// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


if (!$error) {
  ini_set("sendmail_from", $siteOwnersEmail); // for windows server
  $mail = mail($siteOwnersEmail, $subject, $message, $headers);

    if ($mail) { echo "OK"; }
  else { echo "Something went wrong. Please try again."; }

 } # end if - no validation error

 else {
 //there was a validation error
}

有谁知道为什么输入字段在&#34; foodform&#34;不要提交?我现在已经在这方面挣扎了一段时间。

提前致谢

西蒙

1 个答案:

答案 0 :(得分:1)

尝试替换PHP代码:

$volwassenen = trim(stripslashes($_POST['volwassen']));
$kinderen = trim(stripslashes($_POST['kind']));

用这个:

$volwassenen = trim(stripslashes($_POST['volwassenPorties'])); 
$kinderen = trim(stripslashes($_POST['kindPorties']));