简体   繁体   English

没有其他网址,可能执行Ajax网址操作吗?

[英]Ajax url action possible without another url?

I am trying to integrate ajax for not getting the page refreshed when i save something and only get a confirmation message. 我正在尝试集成ajax,以便在保存内容并仅收到确认消息时不刷新页面。 The problem is that I get the confirmation message but data is not inserted in the database. 问题是我收到确认消息,但数据未插入数据库中。

page.inc 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>';
   }
}

I tried with 我尝试过

$(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'); // this is how the data is inserted in the database. //这就是将数据插入数据库的方式。 $insertarray is the array where data is being hold and favoritecar is the table where it goes. $insertarray是保存数据的数组,而favoritecar是数据所在的表。 I want to say that in the old fashion method with <form action="" method="post"> works fine it's inserting the data showing message and reloading. 我想说的是,在具有<form action="" method="post">的旧方法中,它可以很好地插入显示消息的数据并重新加载。

Functions QueryResult and InsertValues are custom functions that look like this: http://pastebin.com/8cpA9R4Q 函数QueryResultInsertValues是如下所示的自定义函数: http : InsertValues

I tried also adding to the url from the ajax url: "page.inc" - the actual page that the code sits on, but it did not echo any success messages. 我还尝试从ajax url中添加URL:“ page.inc”-代码所在的实际页面,但是它没有回显任何成功消息。

Please help me I am a noob in ajax. 请帮助我,我是ajax的菜鸟。 This is my first attempt on implementing ajax. 这是我实现ajax的第一次尝试。 It would be much appreciated. 将不胜感激。

  • Firstly, I'd like to know if your data are correctly retrieve in your php function addtofavorites() ? 首先,我想知道在php函数addtofavorites()中是否正确检索了您的数据? Can you show us your $_POST values ? 您能告诉我们您的$ _POST值吗?
  • For me the action is correctly stop after the form submitting. 对我来说,提交表单后,该操作已正确停止。
  • The url attribute can be empty ? url属性可以为空吗?

  • This will not be an answer to your question but an advice and an another step. 这将不是对您问题的答案,而是建议和下一步。 If you are making an AJAX request, don't put javascript in your PHP code. 如果您要发出AJAX请求,请不要在JavaScript代码中放入javascript。 Ok, it's work but it's ugly. 好的,它可以工作,但是很难看。 I suggest you to return a boolean to know if insertion was a success (or a 'code' like you want). 我建议您返回一个布尔值,以了解插入是否成功(或您想要的“代码”)。

     $("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(); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM