Question

I wrote an Ajax code from admin.php to viewcollege.php where it echo's a jQuery which is not working on 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);
        ?>

So from these two files, when I click 'view college', the button from admin, am getting the code echo'ed from viewcol.php using Ajax, in which I echo'ed jQuery code which is not working.

No correct solution

OTHER TIPS

You need to put your jquery code into the callback function of your ajax call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top