我在使用jQuery的聯繫表單時遇到了一些問題。JQuery窗體保持靜態 - 頁面不會即時驗證
我以前使用過相同的代碼幾次,但這次我更改了divs和類 以獲得有意義的名稱。
一切正常,只是當你按下提交時,你實際上被帶到send.php而不是被放置在佔位符div中的數據。
這讓我很生氣,所以我想知道是否還有幾雙眼睛可以發現一些東西?
感謝,
馬丁
所有JS包括在頁面的頭:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.anchor.js" type="text/javascript"></script>
<script src="js/jquery.fancybox-1.2.6.pack.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.highlight').hover(function(){
$(this).children().addClass('datahighlight');
},function(){
$(this).children().removeClass('datahighlight');
});
});
</script>
<script type="text/javascript">
window.addEvent('domready', function(){
$('contactform').addEvent('submit', function(e) {
new Event(e).stop();
var log = $('form_results').empty().addClass('ajax-loading');
this.send({
update: log,
onComplete: function() {
log.removeClass('ajax-loading');
}
});
});
});
</script>
聯繫部分:
<h2 class="contact">Contact Us</h2>
<p> Some text</p>
<div id="log">
<div id="form_results">
<!-- SPANNER -->
</div>
</div>
<form method="post" id="contactform" action="send.php">
<p><label for="name">Name</label></p>
<input type="text" id=name name=name placeholder="First and last name" tabindex="1" />
<p><label for="email">Email</label></p>
<input type="text" id=email name=email placeholder="[email protected]" tabindex="2" />
<p><label for="comment">Your Message</label></p>
<textarea name="comment" id=comment name=comment placeholder="Comments" tabindex="4"></textarea>
<input name="submit" type="submit" id="submit" tabindex="5" value="Send Message" />
</form>
</section>
Send.php:
<?php
function alpha_numeric($str)
{
return (! preg_match("/^([-a-z0-9])+$/i", $str)) ? FALSE : TRUE;
}
function valid_email($str)
{
return (! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if ($_POST['name']=='' || strlen($_POST['name'])<3)
{
$errors[] = 'Please include your name, it helps when contacting you! [needs to be more than 3 characters]';
}
if (valid_email($_POST['email'])==FALSE)
{
$errors[] = 'At least put a real e-mail address!';
}
if ($_POST['comment']=='' || strlen($_POST['comment'])<3)
{
$errors[] = 'Please write something, otherwise it\'s pointless contacting us!';
}
if(is_array($errors))
{
echo '<div id="error-margin">';
echo '<p class="error"><b>Can\'t send it for the following reasons:</b></p><br />';
while (list($key,$value) = each($errors))
{
echo '<span class="error">'.$value.'</span><br /><br />';
}
echo'</div>';
}
else {
// do something here----store to db, send email
echo'<div id="success">';
echo '<p><b>It worked!</b></p>';
echo '<span>Thanks for contacting us! We\'ll be in touch soon</span>';
echo'</div>';
$name = stripslashes($_POST['name']); //sender's name
$email = stripslashes($_POST['email']); //sender's email
// The subject
$subject = "Message from 4playtheband.co.uk "; //The default subject. Will appear by default in all messages.
$msg = "From : $name \r\n"; // add sender's name to the message
$msg .= "e-mail : $email \r\n\n"; // add sender's email to the message
$msg .= "---Message--- \r\n\n\t".stripslashes($_POST['comment'])."\r\n\n"; // the message itself
// Extras:
// Display user information such as Ip address and browsers information...
$msg .= "---User information--- \r\n\n"; //Title
$msg .= "User IP Address: ".$_SERVER["REMOTE_ADDR"]."\r\n"; //Sender's IP
$msg .= "Browser: ".$_SERVER["HTTP_USER_AGENT"]."\r\n"; //User agent
$msg .= "Page User Came From: ".$_SERVER["HTTP_REFERER"]; //Referrer
// Send!
(mail('someone', $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n"));
}
?>
感謝您抽出寶貴時間提出此建議,但該網頁的表現方式仍然相同。 因爲動作和方法是在js代碼中聲明的,這是否意味着你可以從'
我已更新DEMO&SOURCE我的代碼工作,也是你的PHP代碼工作,因此你可能有某些地方的js衝突。讓我知道。 – 2011-03-16 14:33:42
完美,非常感謝。唯一的區別是我能看到的是,你合併了這兩個JavaScript片段,所以不知道爲什麼它突然發揮作用。 你有任何提示如何使div淡入和淡出? – martincarlin87 2011-03-16 14:38:12