[英]HTML Form using Ajax and PHP not working
只是想知道是否有人可以帮助我?
我正在尝试学习一些Ajax(这种语言令人困惑),我发现问题就像脚本完全被忽略或者我只是犯了一个巨大的业余错误。
在我显示代码之前,我试图制作一个Jsfiddle,但它不允许PHP文件。
HTML:
<form method="post" action="email.php">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
<label for="phone">Phone Number</label>
<input class="form-control" type="phone" id="phone" name="phone" placeholder="Phone Number">
<label for="message">Message</label>
<textarea placeholder="Message" name="comments" id="comments" class="form-control" rows="5"></textarea>
<button type="submit" id="submit_button" class="btn btn-lg btn-success">Send</button>
</form>
PHP(email.php):
<?php
if(isset($_POST['email'])) {
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['phone']; // not required
$comments = $_POST['comments']; // required
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "test@email.com";
//$email_also ="test@yahoo.com";
$email_subject = $name . " Website Inquiry";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
//@mail($email_also, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<!--Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
阿贾克斯:
<script type="text/javascript">
$(document).ready(function() {
$("#submit_button").click, (function() {
// Get all of the values from the input fields through their ID's
var name = $("#name").val();
var email = $("#email").val();
var phone = $("#phone").val();
var comments = $("#comments").val();
// Validate the form to make sure that all of the required fields are not left empty
if(first_name != ''
&& email != ''
&& comments != '')
{
$.ajax({
url: "email.php",
type: "POST",
data: ({
first_name: name,
email: email,
phone: phone,
comments: comments
}),
success: function(data)
{
alert("Message has been received");// You might want to display a message telling the user that the form was successfully filled out.
}
});
}
if(name == ''
|| email == ''
|| comments == '')
{
alert("You left one of the required fields empty");
}
});
});
</script>
最终目标是创建一个在文档上内联运行的php表单,这样就不会刷新页面
如果有人可以帮助它将非常感激。
您的评论textarea输入中没有“name”属性。 试一试。 如果你不使用name属性,我不相信POST会把它拿起来。
另外,请更改此部分
$email_subject = $name + " Website Inquiry";
对...
$email_subject = $name . " Website Inquiry";
PHP使用连接字符串。 javascript使用+
应用@magnified的答案后,当点击提交按钮时,它仍将重定向到email.php作为页面...
这是修复。 这条线
$("#submit_button").click, (function() {
应该是这样的
$("#submit_button").click(function() {
逗号可能是作为拼写错误而来的。 此外,由于您使用ajax提交表单,
<form method="post" action="email.php">
应该
<form>
和
<button type="submit" id="submit_button"
应该
<button id="submit_button"
如果表单中有提交按钮(type =“submit”)和action =“pageUrl”,则单击该按钮将重定向到pageUrl。 您可以取消单击提交按钮时产生的回发:
<script type="text/javascript">
$("#submit_button").click(function(event) {
event.preventDefault(); //cancel postback
// Get all of the values from the input fields through their ID's
var name = $("#name").val();
var email = $("#email").val();
var phone = $("#phone").val();
var comments = $("#comments").val();
if(name != '' && email != '' && comments != '')
{
var postData = {
first_name: name,
email: email,
phone: phone,
comments: comments
};
//ajax:
$.post("email.php", data: postData, function(response){
alert("Message from email.php: " + response);
});
}
else
{
alert("You left one of the required fields empty");
}
});
</script>
或者使用ajax()函数:
<script type="text/javascript">
$(document).ready(function() {
$("#submit_button").click(function(event) {
event.preventDefault();//cancel postback
// Get all of the values from the input fields through their ID's
var name = $("#name").val();
var email = $("#email").val();
var phone = $("#phone").val();
var comments = $("#comments").val();
// Validate the form to make sure that all of the required fields are not left empty
//if(first_name != '' <-- ERROR: variable does not exist.
if(name != '' && email != '' && comments != '')
{
$.ajax({
url: "email.php",
type: "POST",
data: {
first_name: name,
email: email,
phone: phone,
comments: comments
},
success: function(data)
{
// You might want to display a message telling
//the user that the form was successfully filled out.
alert("Message has been received");
}
});
}
else
{
alert("You left one of the required fields empty");
}
});//click()
});//ready()
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.