我对编程很新,但我正在尝试创建一个ajax联系表单。我尝试了很多不同的方法,但似乎没有一种方法能够有效地运作。我的代码如下:
html / js代码:
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<form class="form--square form-email" id="contactformid" method="post" name="contactformid">
<h4 class="text-center">Or you can reach us right here:</h4>
<div class="input-with-icon col-sm-12">
<i class="icon-Business-Man" style="color:#0aaf4b"></i> <input id="name" name="name" placeholder="Name" required="" type="text">
</div>
<div class="input-with-icon col-sm-6">
<i class="icon-Email" style="color:#0aaf4b"></i> <input id="email" name="email" placeholder="Email Address" required="" type="text">
</div>
<div class="input-with-icon col-sm-6">
<i class="icon-Smartphone-3" style="color:#0aaf4b"></i> <input id="phone" name="phone" placeholder="Phone Number" required="" type="text">
</div>
<div class="col-sm-12">
<textarea id="message" name="message" placeholder="Message" required="" rows="8"></textarea>
</div>
<div class="col-sm-12">
<button class="btn btn--primary vpe" id="submitBtn" name="submit" style="width: 100%" type="submit">SEND</button>
</div>
<div id="feedback"></div>
</form>
<script src="http://code.jquery.com/jquery-latest.js">
</script>
<script>
$(document).ready(function(){
$( "#submitBtn" ).click(function( event ) {
//values
var name=$('#name').val():
var email=$('#email').val():
var phone=$('#phone').val():
var message=$('#message').val():
var dataString = {"name": name, "email":email, "phone": phone, "message":message}
console.log(dataString);
$.ajax({
type:"POST",
url:"./newest.php",
data: dataString,
success: function() {
alert("Email Sent Successfully");
}
});
});
});
</script>
</div>
</div>
&#13;
我的php代码:
<?php
if(isset($_POST['name'], $_POST['email'], $_POST['phone'],$_POST['message'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$from = 'Contact Us Page';
$to = 'redwards@farmdrive.co.ke';
$subject = 'Message from Contact Us ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!$_POST['phone']) {
$errEmail = 'Please enter your telephone number';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! We will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
&#13;
截至目前,我收到的是“不允许”#39;如果您有任何建议或意见,请告诉我。