PHP表单返回页首
问题描述:
我的PHP包含一个PHP表单,在我的投资组合中,您可以在这里看到http://www.tinybigstudio.com事情是,在SUBMIT之后,页面进入TOP而不是停留在表单所在的底部。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 sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['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 = '[email protected]'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' .$subject = "You have mail, yes!" . "\r\n" . 'Te lo manda: ' . $name;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
这是形式:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<fieldset>
<label for="name">Name</label>
<input type="text" size="50" name="contactname" id="contactname" value="" class="required">
<label for="email">Email</label>
<input type="text" size="50" name="email" id="email" value="" class="required">
<label for="message">Message</label>
<textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>
<input type="submit" value="S E N D" class="send" name="submit" onmouseover="this.className='send_hover';"
onmouseout="this.className='send';">
</form>
<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Message Successfully Sent!</strong></p>
<p>Thank you <strong><?php echo $name;?></strong> for sending a message!</p>
<?php }
?>
答
你需要一个锚添加到联系人的形式,并以此作为形式action.eg:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>#contact" >
和前右加这个“伪链接”联系表格
<a name="contact">Contact me</a>
立即提交后,用户将直接发送到形式和页面
答
这是因为你将它提交给,你是在同一个页面上,因此,浏览器会有效刷新$ _POST数据。如果你想停止这个,你需要使用AJAX来提交它,以便它发生在后台。看看的API - http://api.jquery.com/jQuery.ajax/
大的不顶!有用! – pablo 2011-06-16 09:21:31
np..btw,美丽的网站;) – 2011-06-16 09:57:01
它是_is_漂亮。虽然你需要处理你的散文。 “文本”,“bussy”,“'”而不是“'”(例如“LET'S GET BUSSY”)会阻止我选择你作为供应商。 – 2011-06-16 10:22:53