我有这个php联系表单,它工作得很好但是当我尝试在其中添加一些无线电代码时;这是行不通的。含义发送电子邮件但不发送无线电部分。我该如何编码才能工作。
的index.html
<body onLoad="randomNums();">
<div class="contactForm">
<br>
<table width="42%">
<tr>
<td width="12%"><strong>Name</strong></td>
</tr>
<tr>
<td width="88%"><input type="text" name="first_name" id="first_name" style="width:374px;"></td>
</tr>
</table>
<br>
<table width="63%">
<tr>
<td width="9%"><strong>Email</strong></td>
<td width="26%"><strong>Phone</strong> (Optional)</td>
</tr>
<tr>
<td width="32%"><input type="text" id="email" name="email"></td>
<td width="33%"><input type="text" id="phone" name="phone"></td>
</tr>
</table>
<br />
<strong>Message</strong>
<br />
<textarea name="message" id="message" rows="12" cols="64"></textarea>
<br />
I'm a: <input type="radio" name="type" value="Other" checked="checked" /> Other
<input type="radio" name="type" value="Student" /> Student
<input type="radio" name="type" value="Faculty" /> Faculty/Staff Member<br />
<br />
<strong>Please add these numbers:</strong>
<span id="digit1"></span> +
<span id="digit2"></span> =
<input type="text" id="answer" size="2">
<br />
<br />
<div id="status"><button type="button" id="myBtn" onClick="ajax_postContact();">Send Now</button></div>
</div>
</body>
Contact.js
function ajax_postContact(){
var msg = document.getElementById("message").value;
var answer = document.getElementById("answer").value;
var fn = document.getElementById("first_name").value;
var ph = document.getElementById("phone").value;
var em = document.getElementById("email").value;
var digit1 = parseInt(document.getElementById("digit1").innerHTML);
var digit2 = parseInt(document.getElementById("digit2").innerHTML);
///// error checking ////
var sum = digit1 + digit2;
if(answer == null || answer == ""){
alert("Please add the numbers");
return false;
}
else if(answer != sum){
alert("Your answer to the math problem is wrong. Please try again.");
return false;
}
else if(fn == null || fn == ""){
alert("Please type your name");
return false;
}
else if(em == null || em == ""){
alert("Please type your email address");
return false;
}
else if(msg == null || msg == ""){
alert("Please leave a message");
return false;
}
else{
var hr = new XMLHttpRequest();
var url = "parse_contact.php";
var vars = "first_name="+fn+"&phone="+ph+"&email="+em+"&message="+msg;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200){
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
hr.send(vars);
document.getElementById("status").innerHTML = "<img src='images/loaderblue.gif' alt='Loader'>";
document.getElementById("first_name").value = "";
document.getElementById("phone").value = "";
document.getElementById("email").value = "";
document.getElementById("message").value = "";
document.getElementById("answer").value = "";
}
}
parse_contact.php
<?php
if(isset($_POST['first_name'])) {
$first_name = $_POST['first_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$msg = $_POST['message'];
$type = $_POST['type'];
$regex = '/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/';
if((!$first_name) || (!$email) || (!$msg)){
echo 'Oops! Please fill in all fields to submit the form';
exit();
}else if(!preg_match($regex, $email)){
echo 'Sorry, that is not a valid email address, please try using another';
exit();
}else{
$first_name = strip_tags(stripslashes($first_name));
$msg = strip_tags(stripslashes($msg));
$phone = preg_replace('/^0-9.-/','',$phone);
$to = "cc-wp@hotmail.com";
$from = $email;
$subject = "You have a message from your website";
///email body ///
$message = "
Name:$first_name Phone:$phone
Email:$email
Feedback Comment:
$msg
$type";
//// set headers ////
$headers = 'MIME-Version: 1.0' . "/r/n";
$headers .= 'Content-type: text' . "/r/n";
$headers .= 'From: $from'. "\r\n";
/// send email now ///
mail($to, $subject, $message, $headers, '-f cc-wp@hotmail.com');
echo "Thanks, for your feedback";
exit();
}
}
?>
答案 0 :(得分:0)
您的POST请求中未包含收音机输入中的type
。
您的代码段:
var vars = "first_name="+fn+"&phone="+ph+"&email="+em+"&message="+msg;
hr.open("POST", url, true);
您正在编码除收音机类型以外的其他输入。
答案 1 :(得分:0)
代码编辑:
function ajax_postContact() {
var msg = document.getElementById("message").value;
var answer = document.getElementById("answer").value;
var fn = document.getElementById("first_name").value;
var ph = document.getElementById("phone").value;
var em = document.getElementById("email").value;
var type = document.getElementsByName("type").value;
var digit1 = parseInt(document.getElementById("digit1").innerHTML);
var digit2 = parseInt(document.getElementById("digit2").innerHTML);
///// error checking ////
var sum = digit1 + digit2;
if (answer == null || answer == "") {
alert("Please add the numbers");
return false;
} else if (answer != sum) {
alert("Your answer to the math problem is wrong. Please try again.");
return false;
} else if (fn == null || fn == "") {
alert("Please type your name");
return false;
} else if (em == null || em == "") {
alert("Please type your email address");
return false;
} else if (msg == null || msg == "") {
alert("Please leave a message");
return false;
} else if (type == null || type == "") {
alert("Please select the type");
return false;
} else {
var hr = new XMLHttpRequest();
var url = "parse_contact.php";
var vars = "first_name=" + fn + "&phone=" + ph + "&email=" + em + "&message=" + msg + "&type=" + type;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if (hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
hr.send(vars);
document.getElementById("status").innerHTML = "<img src='images/loaderblue.gif' alt='Loader'>";
document.getElementById("first_name").value = "";
document.getElementById("phone").value = "";
document.getElementById("email").value = "";
document.getElementById("message").value = "";
document.getElementById("answer").value = "";
document.getElementByName("type").value = "";
}}