Hey guys, Im going to try and explain this the best I can, maybe a bit difficult, but here goes anyway:

I have AJAX calling a php function which displays a random quote when a user clicks a link. Every 5th click a div is echoed over the quotes, that the user can click on.

Once clicked it's supposed to go back to the quotes until the next 5th click.

Here's the code which I'm working with [commented parts to help explain]:

<?php

        session_start();
		
        //If the act variable is valid
        if ($_REQUEST['act'] != 'char_con') die ();
        
        //Make a database connection
        $con = mysql_connect("localhost", "root", "x") or die (json_encode('Could not connect'));
        mysql_select_db("quote", $con) or die (json_encode('Database could not be found'));
        
        //Get a random quote
        $qry = mysql_query("SELECT * FROM quote ORDER BY RAND() LIMIT 1") or die (json_encode('Query error'));
        
        //Set a variable
        $quote = mysql_fetch_array($qry);
        $quote = $quote['q_quote'];
        


        //THIS IS WHERE MY COUNT STARTS



        if (isset($_REQUEST['bc'])){
            $_SESSION['count']++;
            if (isset($_SESSION['count']) && $_SESSION['count'] % 5 == 0)



        // IF IT'S A MULTIPLE OF 5, ECHO THE FOLLOWING:



                die (json_encode('<a href="#" id="xavisys-logo" onClick="myinfo();"></a>'));
        }



        
        //Print the result in json format for the Javascript to handle
        die (json_encode($quote));
		
		        //Close database connection
        mysql_close($con);

?>

And my AJAX:

Function 1

$(document).ready(function(){

        //the link used to change the quote's id = '#generate-quote'

	$("#generate-quote").click(function(e){
							 
			var r1='';
			var r2='';
 
			e.preventDefault();
			 
			$.get("char_con.php?", {act: 'char_con'}, function(data){
															   
			r1=data;

			});
			
			$.getJSON("generator.php?", {act: 'char_con', bc: '1'}, function(data){
	 
			r2=data;
			
			$(".quote").html( r1 + r2 );
 
		});
			
	});
	
});

The code works, but this is the line I'm having trouble with:

die (json_encode('<a href="#" id="xavisys-logo" onClick="myinfo();"></a>'));

You see when the count gets to 5 it does indeed display what it should. The onClick="myinfo(); works as it should, but no matter what I do I can't get it run the AJAX function again when it's been clicked.

I've tried adding the same ajax function to anyting with the id 'xavisys-logo', which works when it's not being echoed, but refuses to work when it's echoed like this.

Does anyone have any suggestions?

If there's a different method I'll be happy to try it, but to sum up:

I want this link:

die (json_encode('<a href="#" id="xavisys-logo" onClick="myinfo();"></a>'));

to run function 1 when it's been clicked. Or return to the function, which ever is easiest.

Any help appreciated. (:

ello.

The code works, but this is the line I'm having trouble with:

die (json_encode('<a href="#" id="xavisys-logo" onClick="myinfo();"></a>'));

You see when the count gets to 5 it does indeed display what it should. The onClick="myinfo(); works as it should, but no matter what I do I can't get it run the AJAX function again when it's been clicked.

Maybe this would help:

$('#xavisys-logo').live('click', function() {
  // Live handler called.
  //do what you want
});

I use live method when there is something reloaded with AJAX, so I guess in your task its the same problem.

You can read more there http://api.jquery.com/live/

Maybe this would help:

$('#xavisys-logo').live('click', function() {
  // Live handler called.
  //do what you want
});

I use live method when there is something reloaded with AJAX, so I guess in your task its the same problem.

You can read more there http://api.jquery.com/live/

Thanks alot, worked perfectly. Never heard of this method before so cheers on the info :) +1

ello

Good to know that I am able to help somebody, not only get help from others :)

Good to know that I am able to help somebody, not only get help from others :)

Well you certainly helped me :)

But there is one tiny problem, now the onclick isn't working, don't suppose you or anyone know why this is?

Any help appreciated :)

I think you can remove onclick and somehow put the code in live click. Its the same as click, only differs that it works when data is refreshed.

I think you can remove onclick and somehow put the code in live click. Its the same as click, only differs that it works when data is refreshed.

I was wondering if I had to do that. Cheers for the info :)

ello.