2015-10-12 176 views
0

我想更新index.php的另一頁,userreg.php。這裏是我的代碼:發送AJAX請求時'ajax函數未定義'

<html> 
<head> 
<title> User List </title> 
    <link rel="stylesheet" href="css/datacss.css" /> 

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
    <script type="text/javascript" src="js/jquery-latest.js"></script> 

<script> 
    $(document).ready(function(){ 
     $("#img1").click(function(){  
      $.ajax({ 
       type: 'POST', 
       url: 'userreg.php',  
       cache: false, 
       success: function(html) { 
        $('html').html(html); 
       } 
      }); 
     }); 
    }); 
    </script> 
    </head> 
    <body> 
<div id= "ajax"> 
    <img id="img1" src="images/bg/adduser.png" onclick="ajaxFunction()"> 
<div class="search"> 
<form action="index.php" method="get"> 
    <input type="text" id="search" name="search" placeholder="Search..." /> 
    <input type="hidden" name="doSearch" value="1"> 
    <input type="submit" id ="img2" name="submit" value="" /></br> 
    <a href="index.php"><img src="images/bg/reset.png" alt="RESET" /></a> 
    </form> 
</div> 

      <table id="mytable" class="tablesorter"> 
        <tbody> 

      <tr> 
           <th id="name" >Name</th> 
           <th id="email">Email</th> 


           <th id="dob">DOB</th> 

           <th id="image">Image</th> 

           <th>EDIT </th> 

           <th>DELETE</th> 


          </tr> 




<?php 
if(isset($_GET['doSearch'])) { 
if($_GET['doSearch']==1) { 

     include'con_search.php'; 

    } 

} 

else{ 
    include('getuserdata.php'); 



} 
?> 


           </tbody> 

         </table> 



</div> 



<div id="pagination"> 
     <div id="pagiCount"> 
      <?php 
       if(isset($pages)) 
       { 
        if($pages > 1) 
        { if($cur_page > $num_links)  // for taking to page 1 // 
         { $dir = "first"; 
          echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.(1).'">'.$dir.'</a> </span>'; 
         } 
         if($cur_page > 1) 
         { 
          $dir = "prev"; 
          echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page-1).'">'.$dir.'</a> </span>'; 
         } 

         for($x=$start ; $x<=$end ;$x++) 
         { 

          echo ($x == $cur_page) ? '<strong>'.$x.'</strong> ':'<a href="'.$_SERVER['PHP_SELF'].'?page='.$x.'">'.$x.'</a> '; 
         } 
         if($cur_page < $pages) 
         { $dir = "next"; 
          echo '<span id="next"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page+1).'">'.$dir.'</a> </span>'; 
         } 
         if($cur_page < ($pages-$num_links)) 
         { $dir = "last"; 

          echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$pages.'">'.$dir.'</a> '; 
         } 
        } 
       } 
      ?> 
     </div> 
    </div> 

</body> 
</html> 

我在Firebug得到一個錯誤:

ReferenceError: ajaxFunction is not defined

+1

你包括'jquery'庫中刪除inline單擊處理? – dreamweiver

+0

是的,我包括

+2

錯誤似乎與你顯示的代碼。 –

回答

2

在此行中:

<img id="img1" src="images/bg/adduser.png" onclick="ajaxFunction()"> 

您指定的ajaxFunction功能應該叫做圖像時被點擊,但該功能不存在。正如你已經使用jQuery的綁定上$("#img1").click的功能,你可以簡單地刪除像這樣的屬性:

<img id="img1" src="images/bg/adduser.png"> 
+0

我沒有得到完整的CSS從加載文件 –

+0

這似乎沒有涉及到你問的問題? –

0

你還沒有在你的腳本,你給點擊事件的回調(在線處理)所定義的方法ajaxFunction()圖像編號爲img1

誤區代碼:

  • 您爲點擊定義了兩個回調處理程序,一個inline和其他內部jQuery的語法document.ready()
  • 由於內聯點擊處理程序需要優先權,因此JS引擎正在嘗試查找ajaxFunction(),該程序不可用。

糾正你的錯誤,只是從圖像標籤ID爲img1