$ ajax正在发送但缺少值

时间:2011-05-12 17:15:44

标签: ajax jquery

好的,所以我的脚本尝试从mysql的php结果请求并将它们加载到div中。结果基于nid值,必须发送此值是从id =“record-#”中提取的,记录被删除,而#留下来由ajax用作nid的id。

这里是ajax数据函数

    $(document).ready(function() {

   $(".note").live('click',function() {
      $("#note_utm_con").show();
      $("#note_utm_nt").html("<img src='http://www.ajaxload.info/images/exemples/4.gif' />");

      $.ajax({
         type: "GET",
         url: "_class/view.php",
         data: "ajax=1&nid=" + parent.attr('id'),
         success: function(html){
            $("#note_utm").html(html);
            $("#note_utm_nt").html("");
         },
         error: function (XMLHttpRequest, textStatus, errorThrown) {$("#note_utm_nt").html(errorThrown);}
      });
   });

   $("#note_utm_con_cls").click(function() {
     $("#note_utm_con").hide();
    });
});

这是本页的其余部分

       <div id="note_utm_con" style="display: none;">
<button id="note_utm_con_cls" style="width: 20px;float: right; padding: 2px;clear: both;">X</button>
<div id="note_utm"></div>
<div id="note_utm_nt"></div>
</div>

<?php

class notes {

     var $host;
     var $username;
     var $password;
     var $table;

     public function display_notes() {

         $q = "SELECT * FROM `notice` ORDER BY nid ASC LIMIT 12";
         $r = mysql_query($q);


         if ( $r !== false && mysql_num_rows($r) > 0 ) {
         while ( $a = mysql_fetch_assoc($r) ) {

             $nid = stripslashes($a['nid']);
             $note = stripslashes($a['note']);
             $type = stripslashes($a['type']);
             $private = stripslashes($a['private']);
             $date = stripslashes($a['date']);
             $author = stripslashes($a['author']);

             if($type == 1) { 
             $type = "posted a comment."; $icon = "http://cdn1.iconfinder.com/data/icons/Basic_set2_Png/16/megaphone.png"; 
             } elseif($type == 2) { 
             $type = "raised a support ticket."; $icon = "http://cdn1.iconfinder.com/data/icons/basicset/help_16.png";
             } elseif($type == 3) {
             $type = "added new photo."; $icon = "http://cdn1.iconfinder.com/data/icons/Basic_set2_Png/16/photo.png";
             } elseif($type == 4) {
             $type = "updated profile details."; $icon = "http://cdn1.iconfinder.com/data/icons/Basic_set2_Png/16/user_info.png";
             } else { }

             $entry_display .= <<<ENTRY_DISPLAY
             <ul class="list">
             <li id="$nid">
             <a href="javascript:;" id="$nid" onClick="retun false;" class="note">
             <img src="$icon" /> 
             $author, 
             $type           
             </a>
             </li>
             </ul>
ENTRY_DISPLAY;

            }

        } else {

            $entry_display = <<<ENTRY_DISPLAY
             <ul class="list">
             <li> 
             <p>
             <img src="http://cdn1.iconfinder.com/data/icons/basicset/monitor_16.png" /> 
             Nothing to display
             </p>
             </li>
             </ul>
ENTRY_DISPLAY;
    }

    return $entry_display;
    }

      public function connect() {
      mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
      mysql_select_db($this->table) or die("Could not select database. " . mysql_error());

      return $this;
      }

      private function note_type() {

             if($type == 1) { $type = "posted a comment!"; } elseif($type == 2) { $type = "raised a support ticket!"; } else { }
             return $type;
      }

}


?>

所以当单击带有class =“note”的LI时,它会触发AJAX调用。然后,调用使用href中的ID从id =“record -

中提取$ nid

在我的情况下,似乎AJAX正在发送请求,因为view.php返回数据应该是的字段,但它似乎没有传递太多需要的nid所以PHP可以从MySQL中选择适当的nid来使用。 / p>

有人可以告诉我它有什么问题以及如何修复它以获取ID?enter code here

2 个答案:

答案 0 :(得分:3)

你的引号错了。试试这个:

data: "ajax=1&nid=" + parent.attr('id').replace('record-',''),

编辑:除非您尚未发布完整代码,否则实际上并未在任何地方设置parent。这意味着您将使用window.parent对象,令人惊讶的是,该对象没有id。您应该使用this.parentNode.id代替:

data: "ajax=1&nid=" + this.parentNode.id,

答案 1 :(得分:2)

从您编写的内容看起来好像您将parent.attr(“id”)替换调用作为字符串传递而不是提取变量:

data: "ajax=1&nid=" + parent.attr('id').replace('record-',''),