为什么jquery代码使用ajax从php回显到另一个页面不起作用?

时间:2013-04-12 21:21:18

标签: php ajax jquery cakephp-ajaxhelper

我写了一个从admin.phpviewcollege.php的Ajax代码,它回显了一个jQuery,它不能用于admin.php

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);
        ?>

所以从这两个文件中,当我点击'查看大学'时,来自admin的按钮,我正在使用Ajax从viewcol.php获取代码回显,其中我回显了jQuery代码,它不能工作

1 个答案:

答案 0 :(得分:0)

您需要将jquery代码放入ajax调用的回调函数中。