2013-04-12 46 views
0

我寫了一個從admin.phpviewcollege.php的Ajax代碼,它回顯了一個jQuery,它不在admin.php上工作。爲什麼jQuery代碼從PHP迴應到另一個頁面使用AJAX不起作用?

admin.php的:

<!-- 
To change this template, choose Tools | Templates 
and open the template in the editor. 
--> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title></title> 
     <script src="jquery-1.9.1.min.js"></script> 
     <script> 
      var i = 1; 
function showHint(str,num) 
{ 


if (str.length==0) 
    { 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
    } 
    } 
    if(num=='1') 
xmlhttp.open("GET","viewcol.php?q="+str,true); 
else if(num=='2') 
    xmlhttp.open("GET","viewbranch.php?q="+str,true); 
    else if (num=='3') 
     { 
     var k=document.getElementById("name").value; 
     xmlhttp.open("GET","moduniv1.php?q="+str+"&r="+k,true); 
     } 
     else 
      xmlhttp.open("GET","subdet.php?q="+str,true); 
xmlhttp.send(); 
} 
</script> 
    </head> 
    <body> 
     <form action="addcol1.php" method="post"> 
     <?php 
     $m=0; 
include 'database.php' ; 
$sql= "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'university'";                                                              $report = mysqli_query($con,$sql); 
$row = mysqli_fetch_array($report); 
//while($row = mysqli_fetch_array($report)) 
    //{ 
    echo "<!-- <input type='radio' name='".$row['TABLE_NAME']."' value='1'> -->University name: ".$row['TABLE_NAME']; 
    echo "<br><br><input type='button' name='".$row['TABLE_NAME']."' id='".$row['TABLE_NAME']."' value='view college' onclick=showHint('".$row['TABLE_NAME']."','1')>" ; 
    echo " <input type='button' name='".$row['TABLE_NAME']."1' id='".$row['TABLE_NAME']."1' value='view branches' onclick=showHint('".$row['TABLE_NAME']."','2')>"; 
    echo " <input type='button' name='moduniv' id='moduniv' value='modify university' />" ; 
    echo " <input type='button' name='subdet' id='subdet' value='subject details'onclick=showHint('".$row['TABLE_NAME']."','4') />" ; 
    echo "<br><p name='pname' id='pname' style='display:none;'>Add Branch: <input type='text' name='name' id='name'><br><input type='button' name='alteruniv' id='alteruniv' value='add' onclick=showHint('".$row['TABLE_NAME']."','3') />" ; 
    echo "<script> 
      $(document).ready(function() 
     { $('#moduniv').click(function(){ 
       $('#pname').toggle(); 
       $('#txtHint').text(''); 
     }); 
     $('#".$row['TABLE_NAME']."').click(function(){ 
       $('#pname').hide(); 

     }); 
     $('#".$row['TABLE_NAME']."1').click(function(){ 
       $('#pname').hide(); 

     }); 
     $('#subdet').click(function(){ 
       $('#pname').hide(); 

     }); 


     }) 
      </script>"; 

//} 
    mysqli_close($con); 
?> 
<br><br> 
<p>Results: <span id='txtHint'></span></p> 
      <!-- <input type="submit" value="delete selected universities"> --> 


     </form> 
    </body> 
</html> 

viewcol.php:

 <?php 
     include 'database.php' ; 
$message=" "; 
$q=$_GET["q"]; 
$sql="select `collegename` from `$q` " ; 
$report = mysqli_query($con,$sql); 
$i=0; 
while($row = mysqli_fetch_array($report)) 
    { 
    $message=$message."<br><input type='radio' name='college' value='".$row['collegename']."'>".$row['collegename']."<br>"; 
    echo $message; 
    $i++; 
    } 

    echo $message; 
    echo "<br><input type='button' name='addcol' id='addcol' value='addcol'><input type='button' name='delcol' id='delcol' value='delete col'>"; 
    echo "<script src='jquery-1.9.1.min.js'></script>"; 
    echo "<script> $(document).ready(function() 
     { $('#addcol').click(function() 
     {jQuery.get('addcol.php?q=',function(data,status) 
     { 
    $('#collegehint').text(data); 
    }); 
     }); 
      $('delcol').click(function() 
     {alert('working'); 

     jQuery.get('delcol.php?q='+$('#college').val()'&r='+".$q.",function(data,status) 
     { 
    $('#collegehint').text(data); 
    showHint('".$q."','1'); 
    }); 
     }); 
     }); 
     </script>"; 
    echo "<br>Result: <p name='collegehint'></p>"; 

mysqli_close($con); 
     ?> 

所以從這兩個文件,當我點擊 '查看大學',從管理的按鈕,我得到的代碼使用Ajax從viewcol.php回顯,其中我回顯了jQuery代碼,該代碼不起作用。

+1

如果您使用jQuery,我不會推薦構建自己的Ajax調用。 –

+0

是的代碼工作時,我試圖在一個新的HTML頁面..但沒有工作時,使用ajax ... –

+0

$(document).ready()是在DOM準備好時觸發的。它不適用於已經準備好的DOM。 –

回答

0

你需要把你的jQuery代碼放到你的ajax調用的回調函數中。

相關問題