我在HTML中使用多个制表符,并使用javascript和PHP将所有值发送到电子邮件。请查看以下代码。我收到这封电子邮件:
Zipcode: Question1: Question2: Question3:
我认为有一些javascript或PHP错误。我在多个标签页中使用表单,并在提交时将值发送到电子邮件。
<form id="regForm" action="" method="post">
<div class="main-form">
<!-- One "tab" for each step in the form: -->
<div class="tab"><h4>Zip Code</h4>
<p><input type="text" class="text1" placeholder="Enter zip code..." oninput="this.className = ''" name="zipcode"></p>
<!--<p><input placeholder="Last name..." oninput="this.className = ''" name="lname"></p> -->
</div>
<div class="tab"><h4>What type of home are you looking to buy?</h4>
<p><input type="radio" name="rdb" value="SingleFamilyHouse" class="option-input radio" checked/> Single Family House</p>
<p><input type="radio" name="rdb" value="Condo" class="option-input radio"> Condo</p>
<p><input type="radio" name="rdb" value="Townhouse" class="option-input radio"> Townhouse</p>
<p><input type="radio" name="rdb" value="Multiunits" class="option-input radio"> Multi-units </p>
</div>
<div class="tab"><h4>How many bedrooms?</h4>
<p><input type="radio" name="rdb1" value="studio" class="option-input radio" checked/> Studio</p>
<p><input type="radio" name="rdb1" value="1-2bedrooms" class="option-input radio"> 1-2 Bedrooms</p>
<p><input type="radio" name="rdb1" value="3-4bedrooms" class="option-input radio"> 3-4 Bedrooms</p>
<p><input type="radio" name="rdb1" value="5ormore" class="option-input radio"> 5 Or More </p>
</div>
<div class="tab"><h4>When do you plan to buy?</h4>
<p><input type="radio" name="rdb2" value="1-3months" class="option-input radio" checked/> 1-3 months</p>
<p><input type="radio" name="rdb2" value="4-6months" class="option-input radio"> 4-6 months</p>
<p><input type="radio" name="rdb2" value="morethan6months" class="option-input radio"> more than 6 months</p>
</div>
<div class="tab"><h4>Do you need to sell before buying?</h4>
<p><input type="radio" name="rdb3" value="yes" class="option-input radio" checked/> Yes</p>
<p><input type="radio" name="rdb3" value="No" class="option-input radio"> No</p>
</div>
<div class="tab"><h4>How much house are you looking at?</h4>
<p><input type="radio" name="rdb4" value="1" class="option-input radio" checked/> $300,00-$500,000</p>
<p><input type="radio" name="rdb4" value="2" class="option-input radio"> $550,000-$750,000</p>
<p><input type="radio" name="rdb4" value="3" class="option-input radio"> $800,000-$1,000,000</p>
<p><input type="radio" name="rdb4" value="4" class="option-input radio"> $1 million up</p>
</div>
<div class="tab"><h4>How much down payment did you plan for?</h4>
<p><input type="radio" name="rdb5" value="lessthan5%" class="option-input radio" checked/> Less than 5%</p>
<p><input type="radio" name="rdb5" value="10%" class="option-input radio"> 10%</p>
<p><input type="radio" name="rdb5" value="20%" class="option-input radio"> 20%</p>
<p><input type="radio" name="rdb5" value="morethan30%" class="option-input radio"> More than 30%</p>
<p><input type="radio" name="rdb5" value="buyingcash" class="option-input radio"> Buying cash</p>
</div>
<div class="tab"><h4>How long have you been in your work or business?</h4>
<p><input type="radio" name="rdb6" value="lessthan2years" class="option-input radio" checked/> Less than 2 Years</p>
<p><input type="radio" name="rdb6" value="2years4 years" class="option-input radio"> 2 Years - 4 Years</p>
<p><input type="radio" name="rdb6" value="5yearsormore" class="option-input radio"> 5 Years or More</p>
</div>
<div class="tab"><h4>Give us an idea of the borrower credit score.</h4>
<p><input type="radio" name="rdb7" value="Needsimprovement" class="option-input radio" checked/> Needs improvement (below 620)</p>
<p><input type="radio" name="rdb7" value="Fair" class="option-input radio"> Fair (620-670)</p>
<p><input type="radio" name="rdb7" value="Good" class="option-input radio"> Good (671-710)</p>
<p><input type="radio" name="rdb7" value="Excellent" class="option-input radio"> Excellent (711-up)</p>
</div>
<div class="tab"><h4>How much is the combined annual income of the borrower/s?</h4>
<p><input type="radio" name="rdb8" value="Less" class="option-input radio" checked/> Less than $30,000</p>
<p><input type="radio" name="rdb8" value="$31" class="option-input radio"> $31,000-$60,000</p>
<p><input type="radio" name="rdb8" value="$61" class="option-input radio"> $61,000-$90,000</p>
<p><input type="radio" name="rdb8" value="$91" class="option-input radio"> $91,000-$120,000</p>
<p><input type="radio" name="rdb8" value="More" class="option-input radio"> More than $120,000</p>
</div>
<div class="tab"><h4>Do you have a home loan pre-approval?</h4>
<p><input type="radio" name="rdb9" value="yes" class="option-input radio" checked/> Yes</p>
<p><input type="radio" name="rdb9" value="No" class="option-input radio"> No</p>
</div>
<div class="tab"><h4>You are almost done!</h4>
<p><input type="text" placeholder="Full name..." oninput="this.className = ''" name="Name"></p>
<p><input type="email" placeholder="E-mail..." oninput="this.className = ''" name="email"></p>
<p><input type="text" placeholder="Phone..." oninput="this.className = ''" name="phone"></p>
</div>
<div class="prevBtn-btm">
<div class="btn-bottom">
<button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button>
<button type="button" id="nextBtn" onclick="nextPrev(1)">Next</button>
</div>
</div>
<!-- Circles which indicates the steps of the form: -->
<div class="step-btn">
<span class="step"></span>
<span class="step"></span>
<span class="step"></span>
<span class="step"></span>
<span class="step"></span>
<span class="step"></span>
<span class="step"></span>
<span class="step"></span>
<span class="step"></span>
<span class="step"></span>
<span class="step"></span>
</div>
</div>
</form>
var currentTab = 0; // Current tab is set to be the first tab (0)
showTab(currentTab); // Display the crurrent tab
function showTab(n) {
// This function will display the specified tab of the form...
var x = document.getElementsByClassName("tab");
x[n].style.display = "block";
//... and fix the Previous/Next buttons:
if (n == 0) {
document.getElementById("prevBtn").style.display = "none";
} else {
document.getElementById("prevBtn").style.display = "inline";
}
if (n == (x.length - 1)) {
document.getElementById("nextBtn").innerHTML = "Submit";
} else {
document.getElementById("nextBtn").innerHTML = "Next";
}
//... and run a function that will display the correct step indicator:
fixStepIndicator(n)
}
function nextPrev(n) {
// This function will figure out which tab to display
var x = document.getElementsByClassName("tab");
// Exit the function if any field in the current tab is invalid:
if (n == 1 && !validateForm()) return false;
// Hide the current tab:
x[currentTab].style.display = "none";
// Increase or decrease the current tab by 1:
currentTab = currentTab + n;
// if you have reached the end of the form...
if (currentTab >= x.length) {
// ... the form gets submitted:
document.getElementById("regForm").submit();
return false;
}
// Otherwise, display the correct tab:
showTab(currentTab);
}
function validateForm() {
// This function deals with validation of the form fields
var x, y, i, valid = true;
x = document.getElementsByClassName("tab");
y = x[currentTab].getElementsByTagName("input");
// A loop that checks every input field in the current tab:
for (i = 0; i < y.length; i++) {
// If a field is empty...
if (y[i].value == "") {
// add an "invalid" class to the field:
y[i].className += " invalid";
// and set the current valid status to false
valid = false;
}
}
// If the valid status is true, mark the step as finished and valid:
if (valid) {
document.getElementsByClassName("step")[currentTab].className += " finish";
}
return valid; // return the valid status
}
function fixStepIndicator(n) {
// This function removes the "active" class of all steps...
var i, x = document.getElementsByClassName("step");
for (i = 0; i < x.length; i++) {
x[i].className = x[i].className.replace(" active", "");
}
//... and adds the "active" class on the current step:
x[n].className += " active";
}
</script>
<?php
$EmailFrom = "test@gmail.com";
$EmailTo = "parmdeep87@gmail.com";
$Subject = "Test";
$zipcode = $_POST['zipcode'];
$rdb_value = trim(stripslashes($_POST['rdb']));
$rdb_value1 = trim(stripslashes($_POST['rdb1']));
$rdb_value2 = trim(stripslashes($_POST['rdb2']));
$rdb_value3 = trim(stripslashes($_POST['rdb3']));
$rdb_value4 = trim(stripslashes($_POST['rdb4']));
$rdb_value5 = trim(stripslashes($_POST['rdb5']));
$rdb_value6 = trim(stripslashes($_POST['rdb6']));
$rdb_value7 = trim(stripslashes($_POST['rdb7']));
$rdb_value8 = trim(stripslashes($_POST['rdb8']));
$rdb_value9 = trim(stripslashes($_POST['rdb9']));
$Name = trim(stripslashes($_POST['Name']));
$email = trim(stripslashes($_POST['email']));
$phone = trim(stripslashes($_POST['phone']));
// prepare email body text
$Body = "";
$Body .= "zipcode: ";
$Body .= $zipcode;
$Body .= "\n";
$Body .= "What type of home are you looking to buy?: ";
$Body .= $rdb_value;
$Body .= "\n";
$Body .= "How many bedrooms?: ";
$Body .= $rdb_value1;
$Body .= "\n";
$Body .= "When do you plan to buy?: ";
$Body .= $rdb_value2;
$Body .= "\n";
$Body .= "Do you need to sell before buying?: ";
$Body .= $rdb_value3;
$Body .= "\n";
$Body .= "How much house are you looking at?: ";
$Body .= $rdb_value4;
$Body .= "\n";
$Body .= "How much down payment did you plan for?: ";
$Body .= $rdb_value5;
$Body .= "\n";
$Body .= "How long have you been in your work or business?: ";
$Body .= $rdb_value6;
$Body .= "\n";
$Body .= "Give us an idea of the borrower credit score.: ";
$Body .= $rdb_value7;
$Body .= "\n";
$Body .= "How much is the combined annual income of the borrower/s?: ";
$Body .= $rdb_value8;
$Body .= "\n";
$Body .= "Do you have a home loan pre-approval?: ";
$Body .= $rdb_value9;
$Body .= "\n";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Phone No.: ";
$Body .= $phone;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
答案 0 :(得分:0)
你的javascript验证逻辑正在检查空值。对于无线电需要使用.checked。
您的代码:
y = x[currentTab].getElementsByTagName("input");
// A loop that checks every input field in the current tab:
for (i = 0; i < y.length; i++) {
// If a field is empty...
if (y[i].value == "") {
// add an "invalid" class to the field:
y[i].className += " invalid";
// and set the current valid status to false
valid = false;
}
}
新代码:
y = x[currentTab].getElementsByTagName("input[type=text]");
// A loop that checks every input field in the current tab:
for (i = 0; i < y.length; i++) {
// If a field is empty...
if (y[i].value=="") {
// add an "invalid" class to the field:
y[i].className += " invalid";
// and set the current valid status to false
valid = false;
}
}
z =
x[currentTab].getElementsByTagName("input[type=radio]");
for(i=0;i<z.length;i++){
if(z[i].checked){
radioval[] = z[i].value;
}
}
如果标记名没有拉起无线电尝试id或classname。你也可以添加一些简单的验证。