【发布时间】:2016-03-22 14:34:29
【问题描述】:
我在我的网站上使用了一个简单的 PHP 联系表单,问题是当我发送消息时,单击我的提交按钮会将我返回到主页。我想避免这种情况并留在页面上。
这是我的代码:
头文件 PHP:
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the phone field is not empty
if(trim($_POST['phone']) == '') {
$hasError = true;
} else {
$phone = trim($_POST['phone']);
}
//Check to make sure that the name field is not empty
if(trim($_POST['weburl']) == '') {
$hasError = true;
} else {
$weburl = trim($_POST['weburl']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!filter_var( trim($_POST['email'], FILTER_VALIDATE_EMAIL ))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'myemail@email.com'; // Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nPhone Number: $phone \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
表格:
<div class="container">
<div class="row">
<div class="col-md-6 col-md-push-3">
<form role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<fieldset>
<legend>Send Us a Message</legend>
<?php if(isset($hasError)) { //If errors are found ?>
<p class="alert alert-danger">Please check if you've filled all the fields with valid information and try again. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<div class="alert alert-success">
<p><strong>Message Successfully Sent!</strong></p>
<p>Thank you for using our contact form, <strong><?php echo $name;?></strong>! Your email was successfully sent and we’ll be in touch with you soon.</p>
</div>
<?php } ?>
<div class="form-group">
<label for="name">Your Name<span class="help-required">*</span></label>
<input type="text" name="contactname" id="contactname" value="" class="form-control required" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="phone">Your Phone Number<span class="help-required">*</span></label>
<input type="text" name="phone" id="phone" value="" class="form-control required" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="email">Your Email<span class="help-required">*</span></label>
<input type="text" name="email" id="email" value="" class="form-control required email" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="weburl">Your Website<span class="help-required">*</span></label>
<input type="text" name="weburl" id="weburl" value="" class="form-control required url" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<select name="subject" id="subject" class="form-control required" role="select" aria-required="true">
<option></option>
<option>One</option>
<option>Two</option>
</select>
</div>
<div class="form-group">
<label for="message">Message<span class="help-required">*</span></label>
<textarea rows="8" name="message" id="message" class="form-control required" role="textbox" aria-required="true"></textarea>
</div>
<div class="actions">
<input type="submit" value="Send Your Message" name="submit" id="submitButton" class="btn btn-primary" title="Click here to submit your message!" />
<input type="reset" value="Clear Form" class="btn btn-danger" title="Remove all the data from the form." />
</div>
</fieldset>
</form>
</div><!-- col -->
</div><!-- row -->
<hr>
<div class="footer">
<p>© Company 2013</p>
</div>
</div> <!-- /container -->
JS:
/* Bootstrap Contact Form
***************************************************************************/
$(document).ready(function(){
// validate signup form on keyup and submit
var validator = $("#contactform").validate({
errorClass:'has-error',
validClass:'has-success',
errorElement:'div',
highlight: function (element, errorClass, validClass) {
$(element).closest('.form-control').addClass(errorClass).removeClass(validClass);
},
unhighlight: function (element, errorClass, validClass) {
$(element).parents(".has-error").removeClass(errorClass).addClass(validClass);
},
rules: {
contactname: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
weburl: {
required: true,
url: true
},
phone: {
required: true,
phoneUS: true
},
subject: {
required: true
},
message: {
required: true,
minlength: 10
}
},
messages: {
contactname: {
required: '<span class="help-block">Please enter your name.</span>',
minlength: jQuery.format('<span class="help-block">Your name needs to be at least {0} characters.</span>')
},
email: {
required: '<span class="help-block">Please enter a valid email address.</span>',
minlength: '<span class="help-block">Please enter a valid email address.</span>'
},
weburl: {
required: '<span class="help-block">You need to enter the address to your website.</span>',
url: jQuery.format('<span class="help-block">You need to enter a valid URL.</span>')
},
phone: {
required: '<span class="help-block">You need to enter your phone number.</span>',
phoneUS: jQuery.format('<span class="help-block">You need to enter a valid phone number.</span>')
},
subject: {
required: '<span class="help-block">You need to enter a subject.</span>'
},
message: {
required: '<span class="help-block">You need to enter a message.</span>',
minlength: jQuery.format('<span class="help-block">Enter at least {0} characters.</span>')
}
}
});
});
我认为
可能有问题<form role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
通过使用
action="<?php echo $_SERVER['PHP_SELF']
我已尝试删除它,也尝试使用 action="#" 但没有效果,提交按钮仍将我重定向到登录页面。
【问题讨论】:
-
是否有任何形式的 URL 重写?包含表单的页面的 URL 是什么?
-
@Salketer 整个网络只有一个主 URL,联系表格放在没有 URL 的滑出面板中
-
肯定有一些 URL 重写正在进行,您向我们展示的似乎没有任何代码正在这样做。
-
表单已经被BootStrap摇动过,实际上根本没有
action=。看一下页面源。查看您的引导程序/javascript 代码 -
@RiggsFolly 是我的错,我将它的不同版本更新到我的域中,打算用行动尝试它,但是当我在 localhost 上尝试一切时,没有任何效果
标签: php jquery html forms twitter-bootstrap