PHP script runs but not when loaded via AJAX

Discussion in 'PHP' started by danramosd, Jun 23, 2010.

  1. #1
    I have a php script that runs fine when the page is originally loaded. When the user clicks on the link the jQuery gets the value of that link, uses ajax to send the selected item to a PHP script and based upon that value it calls a certain function. My problem is that the php script and functions work fine when the page originally loads but when I try and load it via AJAX nothing happens. Here is my PHP code:
    
    function getOverallRatings()
    {
    
    	$c = new DB();  
    
        $c->connectToMySQL();
        
        $sql="SELECT fname,lname,sport,state, id, overall FROM coach WHERE numofratings >5 ORDER BY overall DESC LIMIT 10";	
    	$i=1;
    	$rateResult = array();
    	
    	$result=$c->queery($sql);
    	echo "<table id='resultsTable'><tr><th>Name</th><th>Sport</th><th>State</th><th>Rating</th></tr>";
    
        	while ($row = mysql_fetch_assoc($result)) {
        	echo "<tr><td>".$i.". <a href='person.php?id={$row['id']}'>".$row['fname']." ".$row['lname']."</a></td><td>".$row['sport'].
        	"</td><td>".$row['state']."</td><td>".$row['overall']."</td></tr>";
        	$i++;
      
        	}
      echo "</table>";
      
        	
    
    }
    function getOverallPersonality()
    {
    
    	$c = new DB();  
        $c->connectToMySQL();
        
        $sql="SELECT fname,lname,sport,state, id, overallpersonality FROM coach WHERE numofratings >5 ORDER BY overallpersonality DESC LIMIT 10";	
    	$i=1;
    	$rateResult = array();
    	
    	$result=$c->queery($sql);
    echo "<table id='resultsTable'><tr><td>Name</td><td>Sport</td><td>State</td><td>Rating</td></tr>";
    
        	while ($row = mysql_fetch_assoc($result)) {
        	echo "<tr><td>".$i.". <a href='person.php?id={$row['id']}'>".$row['fname']." ".$row['lname']."</a></td><td>".$row['sport'].
        	"</td><td>".$row['state']."</td><td>".$row['overallpersonality']."</td></tr>";
        	$i++;
      
        	}
      echo "</table>";
    
    }
    
    if(isset($_POST['type'])){
    
    switch ($_POST['type']){
    
    	case 'overall':
    	echo "overall";
    	getOverallRatings(); 
    	break;
    	
    	case 'overallpersonality':
    	echo "personality";
    	getOverallPersonality();
    	break;
    
    
    }
    
    Code (markup):
    $(document).ready(function(){
    
    $(".type").click(function(){		
    	dataString='type='+$(this).attr("id");
    	$.post("/includes/functions.php", dataString,
       function(data){
         alert("Data Loaded: " + data);
       });
    	
    	
    	});
    
    
    });
    Code (markup):
    When i click on the type class i know it selects the correct function because the PHP script echos out 'overall' or 'personality'. The only problem is the function does not get executed. Can anyone tell me why the functions wont work? Does it have something to do with opening a database connection twice from the same page?
     
    danramosd, Jun 23, 2010 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    I don't see any obvious reason for your functions not to work. In such cases I usually put die("here") in different places to see where script stops working.

    One more thing. According to the name o script /includes/functions.php it looks like it is included when generating html page. But when you call in in ajax some needed files may not be included such as your database class or config files.
    Try echo php errors to see details.
     
    AsHinE, Jun 24, 2010 IP
  3. danramosd

    danramosd Active Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    83
    #3
    Ahhh i could be wrong but i think you nailed it on the head w/ the database class being loaded in my html page but not loaded via ajax. I'm at work now but will try this when I get home... thank you for the help!
     
    danramosd, Jun 24, 2010 IP