我有以下ajax调用:
$( document ).ready(function() {
$( "#fform" ).submit(function( event ) {
//alert( "Handler for .submit() called." );
event.preventDefault();
$.ajax({
type: "POST",
url: "apply.php",
data: "email="+$("#emaill").val()
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
}).fail(function() {
alert("Sorry. Server unavailable. ");
});
});
});
和apply.php:
$con=mysqli_connect("some_server","some_usename","some_pass","some_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$r=mysqli_query($con,"INSERT INTO some_table (email, join_date) VALUES ('".$_POST['email']."', CURDATE() )");
if($r)
{
echo 'true';
}
else
{
echo 'false';
}
mysqli_close($con);
奇怪的是,我收到了数据库中的电子邮件,但我没有得到任何警报!请帮忙!
更新:
表格:
<form method="POST" id="fform" >
<fieldset class="clearfixx" style="width: 552px; height: 40px;">
<div class="lp-pom-form-field clearfixx" style="width: 552px; height: 42px; top: 0px;">
<input placeholder="Email Address" type="email" id="emaill" name="email" class="textt" style="top: 0px; left: 58px; width: 520px; font-size: 15px; height: 38px; padding-left: 11px; padding-right: 11px; lineheight: 15px;" required>
</div>
</fieldset>
<input type="submit" class="lp-element lp-pom-button" id="lp-pom-button-25" value="Submit dude"/>
</form>
单击提交按钮后,我在DB上获取数据,但是我没有收到任何警报。谢谢
更新2:
错误:未捕获TypeError:对象#没有方法'完成'
已解决
我使用旧版JQuery谢谢大家
答案 0 :(得分:0)
如果提交了表单#fform,那么ajax函数才会起作用。
在表单中输入提交按钮:
<input type="submit" value="submit" />
点击提交按钮应触发您的功能
答案 1 :(得分:0)
你的jQuery AJAX调用中有一点错误,试试这个:
$.ajax({
type: "POST",
url: "apply.php",
data: "email="+$("#emaill").val()
}).done(function( msg ) {
alert( "Data Saved: " + msg );
}).fail(function() {
alert("Sorry. Server unavailable. ");
});
我建议您使用像firebug或chrome developper工具这样的调试工具,它可以轻松检测到这种语法错误。