我正在尝试集成ajax,以便在我保存某些内容时只获得确认消息时不刷新页面。问题是我收到了确认消息,但未在数据库中插入数据。
page.inc
public function addtofavorites() {
$q = "select * from carads where STATUS='1' and DEL='0' and ADID=".$_REQUEST['carID'];
$result = $this->QueryResult($q);
if($result[0]->TITLE!='-1' || $result[0]->TITLE!='') {
$title = $result[0]->TITLE;
} else {
$title = 'No Title';
}
if($result[0]->IMAGE1!='-1' || $result[0]->IMAGE1!='') {
$image = $result[0]->IMAGE1;
} else {
$image = 'uploads/noimg.jpg';
}
include("inc/membersite_config.php");
$email = $fgmembersite->UserEmail();
$carlink = $_SERVER['REQUEST_URI'];
$carlink = explode('_', $carlink);
$carlink = $carlink[1];
$insertarray = array(
'email' => $email,
'favoritecarlink' => $carlink,
'favoritecartitle' => $title,
'favoritecarimg' => $image);
$q2 = "select * from favoritecar WHERE email='$email'";
$resultfav = $this->QueryResult($q2);
foreach ($resultfav as $resultfavs) {
$carlinkdbs[] = $resultfavs->favoritecarlink;
}
if (!empty($carlinkdbs)) {
if (in_array($carlink, $carlinkdbs)) {
echo '<script type="text/javascript">$(document).ready(function(){ $("#addtofavs").click(function(){ $("#hidemes").text("Car had been already added to your favorite cars"); }); $("#addtofav").blur(function(){ $("#hidemes").text(""); }); });</script>';
echo '<input type="submit" name="addtofav" onclick="displaymessage()" id="addtofavs" class="addtofav" value="">';
} else {
if (isset($_POST["addtofav"])) {
$this->InsertValues($insertarray, 'favoritecar');
echo '<script type="text/javascript">alert("Car had been added to favorites");</script>';
}
echo '<script>$(function () { $("form").on("submit", function (e) { $.ajax({type: "post", url: "", data: $("form").serialize(), success: function () { alert("form was submitted"); } }); e.preventDefault(); }); });</script>';
echo '<form>';
echo '<input type="submit" name="addtofav" onclick="displaymessage()" id="addtofavs" class="addtofav" value="">';
echo '</form>';
}
} else {
if (isset($_POST["addtofav"])) {
$this->InsertValues($insertarray, 'favoritecar');
echo '<script type="text/javascript">alert("Car had been added to favorites");</script>';
}
echo '<form>';
echo '<input type="submit" name="addtofav" onclick="displaymessage()" id="addtofavs" class="addtofav" value="">';
echo '</form>';
}
}
我试过
$(function () {
$("form").on("submit", function (e) {
$.ajax({type: "post", url: "", data: $("form").serialize(), success: function(){ alert("form was submitted"); } }); e.preventDefault(); }); });
$this->InsertValues($insertarray, 'favoritecar');
//这就是数据在数据库中的插入方式。 $insertarray
是保存数据的数组,favoritecar
是表格所在的表。我想说,在<form action="" method="post">
的旧方法中工作正常,它会插入显示消息和重新加载的数据。
函数QueryResult
和InsertValues
是自定义函数,如下所示:
http://pastebin.com/8cpA9R4Q
我还尝试添加来自ajax url的url:“page.inc” - 代码所在的实际页面,但它没有回显任何成功消息。
请帮助我,我是ajax的菜鸟。这是我第一次尝试实现ajax。非常感谢。
答案 0 :(得分:1)
url属性可以为空吗?
这不是你问题的答案,而是建议和另一步。如果您要发出AJAX请求,请不要在您的PHP代码中添加javascript。好吧,这是工作,但它很难看。我建议你返回一个布尔值来知道插入是否成功(或者你想要的'代码')。
$("form").on("submit", function (e) {
$.ajax({
type: "post",
url: "",
data: $("form").serialize(),
success: function(data){
// Check data response... if true insert was a success else an error occurred
}
});
e.preventDefault();
});