Could anyone just help me out with this. I'm trying to get my AJAX to simply echo what's in my php file when a click a link:

AJAX:

$(document).ready(function(){

	$("#gentest").click(function(e){

		e.preventDefault();

		$.getJSON("char_con.php?", {act: 'char_con'}, function(data){

			$(".x").html(data);
		});
                                        

	});
                                
                                
});


                <div class="box">
                        <a href="#" id="gentest">Generate Quote</a>
                        <div class="x">TEST</div>
                </div>

my PHP file:

<?php
		if($_GET['act'] == "char_con") {
		
		$strA ='
		<div id="dark_ad"><img src="img/data/under_ad.png" alt="Click here to begin"></div>
		<div id="con_sep"></div>
		'; 
		}  
		else 
		{
		$strB =' 
		<a href="index.php?act=gen_con"><img src="img/vpbg/vpbgt.png" alt="Click here to begin"></a>
		';
		}
?>

I'm pretty sure I have the AJAX bit down but would anyone tell me how to get it to actually echo the php? I'm a bit lost.

Thanks for any help.

Ello,

The PHP doesn't actually output anything, and what you want to be output isn't JSON encoded.

Try:

<?php
	if($_GET['act'] == "char_con")
	{
		echo '<div id="dark_ad"><img src="img/data/under_ad.png" alt="Click here to begin"></div><div id="con_sep"></div>';
	}
	else
	{
		echo '<a href="index.php?act=gen_con"><img src="img/vpbg/vpbgt.png" alt="Click here to begin"></a>';
	}
?>

and

$(document).ready(function(){
	$("#gentest").click(function(e) {
		e.preventDefault();
		$.get("char_con.php?", {act: 'char_con'}, function(data) {
			$(".x").html(data);
		});
	});
});

Airshow

Ahhh of course, stupidly overlooked the fact I was using $str. Thanks for the fix, worked perfectly (:

Thanks a lot, appreciate it.

+1 for your help. (:

Ello.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.