gpt4 book ai didi

javascript - Ajax jQuery 表单无法正常工作,导航到 PHP 文件

转载 作者:行者123 更新时间:2023-11-29 21:47:18 25 4
gpt4 key购买 nike

我在使用 ajaxForm 插件时遇到问题。我希望它在不离开页面的情况下向用户显示结果消息。

问题是,当我提交表单时,它没有将警报添加到 .jumbotron.modded(这是表单的 div 容器)中,而是导航到 supportForm.php 并只显示回显的数字。

这有时效果很好,但大多数时候它只是导航到 supportForm.php 。我不明白为什么它并不总是具有相同的行为。

这是 HTML 表单:

        <form id="supportForm" action="supportForm.php" method="post" style="padding-top:10px">
<div class="form-group">
<input type="text" name="subject" id="subject" placeholder="Subject" class="form-control" required>
</div>
<div class="form-group">
<textarea class="form-control" rows="4" name="comment" id="comment" placeholder="Write your message here" required></textarea>
</div>
<button type="submit" class="btn btn-upload" style="margin-top:20px;float:right;" value="">SEND<img src="img/plus.png" style="margin-bottom:5px;padding-left:5px;" /></button>
</form>

这是 Javascript 代码:

    window.onload = function()
{
$('#supportForm').ajaxForm({
success: function(res) {
console.log(res);
if(res=='1')
{
$(".jumbotron.modded").prepend('<div class="alert alert-success alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>Successfully sent. Thank you</div>');
}else{
switch(res)
{
case '0':
$(".jumbotron.modded").prepend('<div class="alert alert-danger alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>Error 1. Please contact mail@asd.com<</div>');
break;
case '-1':
$(".jumbotron.modded").prepend('<div class="alert alert-danger alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>Error 2. Please contact mail@asd.com<</div>');
break;
case '-2':
$(".jumbotron.modded").prepend('<div class="alert alert-danger alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>Error 3. Please contact mail@asd.com</div>');
break;
case '-3':
$(".jumbotron.modded").prepend('<div class="alert alert-danger alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>Error 4. Please contact mail@asd.com<</div>');
break;
}
}
}
});
}

这是 PHP 代码:

if ($row = $db->query("SELECT user FROM tblusers WHERE userId='".$fromId."'")->fetch_array())
{
$fromMail1 = $row['user'];
if($row2 = $db->query("SELECT name,lastname,email FROM tblprofile WHERE userId='".$fromId."'")->fetch_array())
{
$fromMail2 = $row2['email'];
$fromName = $row2['name']." ".$row2['lastname'];
if ($db->query("INSERT INTO tblSupports (supportTitle,supportComment,userId) VALUES ('".$title."','".$comment."','".$fromId."')"))
{
if(sendEmail($fromMail1,$fromMail2,$fromName,$titleClean,$commentClean))
{
echo 1;
}else{// (error 1)
echo 0;
}
}else{//(error 2)
echo -1;
}
}else{//(error 3)
echo -2;
}
}else{//(error 4)
echo -3;
}

我该如何解决这个问题?非常感谢

最佳答案

试试这个

$('#supportForm').ajaxForm({
/* bla-bla-bla*/

return false;
})

但是我真的不明白,你用这个插件是为了什么。无需任何插件即可轻松完成。

$('#submit').click(function(){
$.ajax({
method: "POST",
url: "some.php",
data: $('#supportForm').serialize(),
success: function(data){
alert(data)
}
error: function(error){
alert(error);
}
});
return false;
});

像这样。

关于javascript - Ajax jQuery 表单无法正常工作,导航到 PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30651268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com