让我提前道歉,我只是像昨天一样跳进了jquery的事情,所以说慢点并且使用很多初学者的术语。无论如何,我试图让我的jquery / ajax联系表单停止刷新并保持在同一页面,但我一直收到错误,读取“ReferenceError:$未定义”。我试图解决这个问题并弄清楚它是否与我在这里的失败有关。
这是HTML ......
<div id="contact">
<h2>Contact</h2>
<div class="clear"></div>
<div class="contactContainer">
<h3>Get in touch:</h3>
<div class="contact">
<form action="js/ajaxcontactform/sendmail.php" method="post" id="contactForm">
<ul>
<li>
<label for="name">Name:<font color="#ff3300">*</font></label>
<input type="text" name="name" value="" id="name" />
</li>
<li>
<label for="email">Email:<font color="#ff3300">*</font></label>
<input type="text" name="email" value="" id="email" />
</li>
<li>
<label for="tele">Telephone:</label>
<input type="text" name="tele" value="" id="tele" />
</li>
<li class="special" style="display: none;">
<label for="last">Don't fill this in:</label>
<input type="text" name="last" value="" id="last" />
</li>
<li>
<label for="message">Message:<font color="#ff3300">*</font></label><textarea rows="5" name="message"></textarea>
</li>
<li class="submitbutton">
<input type="submit" class="btn" value="Send Message" />
</li>
</ul>
</form>
这是php .....
<?php
// basic settings section
$sendto = 'your@email.com';
$subject = 'You have a new message from your virtual resume!';
$iserrormessage = 'There was a problem with sending e-mail to us, please check:';
$thanks = "Thank's for your message! I'll contact you as soon as possible!";
$emptyname = 'Did you enter your name?';
$emptyemail = 'Did you enter your e-mail address?';
$emptymessage = 'Did you enter the message?';
$emptyphone = 'Did you enter phone number?';
$alertname = 'Please enter your name with standard alphabet!';
$alertemail = 'Please enter your e-maill address in format: name@domain.com';
$alertmessage = "Please do not use any parenthesis or other escaping characters. Standard web url's should work fine!";
$alertphone = 'Please enter your phone number without any special characters, only numbers ex: 5553212';
$alert = '';
$iserror = 0;
// cleaning the post variables
function clean_var($variable) {$variable = strip_tags(stripslashes(trim(rtrim($variable))));return $variable;}
// validation of filled form
if ( empty($_REQUEST['name']) ) {
$iserror = 1;
$alert .= "<li>" . $emptyname . "</li>";
} elseif ( ereg( "[][{}()*+?.\\^$|]", $_REQUEST['name'] ) ) {
$iserror = 1;
$alert .= "<li>" . $alertname . "</li>";
}
if ( empty($_REQUEST['email']) ) {
$iserror = 1;
$alert .= "<li>" . $emptyemail . "</li>";
} elseif ( !eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_REQUEST['email']) ) {
$iserror = 1;
$alert .= "<li>" . $alertemail . "</li>";
}
if ( empty($_REQUEST['message']) ) {
$iserror = 1;
$alert .= "<li>" . $emptymessage . "</li>";
} elseif ( ereg( "[][{}*+\\^|]", $_REQUEST['message'] ) ) {
$iserror = 1;
$alert .= "<li>" . $alertmessage . "</li>";
}
// if there was error, print alert message
if ( $iserror==1 ) {
echo "<script type='text/javascript'>$(\".message\").hide(\"slow\").fadeIn(\"slow\").delay(5000).fadeOut(\"slow\"); </script>";
echo "<strong>" . $iserrormessage . "</strong>";
echo "<ul>";
echo $alert;
echo "</ul>";
} else {
// if everything went fine, send e-mail
$msg = "From: " . clean_var($_REQUEST['name']) . "\n";
$msg .= "Email: " . clean_var($_REQUEST['email']) . "\n";
$msg .= "Message: \n" . clean_var($_REQUEST['message']);
$header = 'From:'. clean_var($_REQUEST['email']);
mail($sendto, $subject, $msg, $header);
echo "<script type='text/javascript'>$(\".message\").fadeOut(\"slow\").fadeIn(\"slow\").animate({opacity: 1.0}, 5000).fadeOut(\"slow\");</script>";
echo $thanks;
die();
}
?>
我正在使用标准的jquery.form.js(据我所知)
提前再次感谢!
答案 0 :(得分:1)
你导入jquery lib吗? 试着把它放在你的代码头中。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
答案 1 :(得分:1)
查看您的实际网站,我发现在您指定的文件夹中找不到所有JS包含的内容。特别是jquery.form.js
的情况。由于找不到此插件,因此在您提交表单时页面会自然刷新。
查看HTML,我找到了这一行:
<script type="text/javascript" src="/js/ajaxcontactform/jquery.form.js"></script>
如果您查找http://www.robergeaz.com/js/ajaxcontactform/jquery.form.js
,您会发现它不存在。
看着你的其他JS位置,我看到他们中的大多数使用相对路径js/xyz.js
,相当于http://www.robergeaz.com/darin/js/xyz.js
。使用与表单插件的其他插件相同的相对路径会在http://www.robergeaz.com/darin/js/ajaxcontactform/jquery.form.js
显示有效位置。
因此,要解决您的问题,请将上述脚本更改为:
<script type="text/javascript" src="js/ajaxcontactform/jquery.form.js"></script>
请注意/
开头缺少的src
。
编辑:
此外,您需要在包含主jQuery库之后加载插件。