Jump to content

reload content via ajax


ngrmm
 Share

Recommended Posts

my ajax-reload leads into a 500 error (console)
am i missing something?

i just want to reload the random content of the the #data-div

template-markup:

if(!$config->ajax) {
	
	include("./inc/header.inc");
	
	echo "<a href='$page->url' class='button' >PRESS THE BUTTON</a>";
	
	echo "<section class='content' id='data'>";
		// some images
	echo "</section>";
	
	include("./inc/footer.inc");	 
}  

else {
	echo "<a href='$page->url' class='button' >PRESS THE BUTTON</a>";
	echo "<section class='content' id='data'>";
		// some images
	echo "</section>";
} 

and js:
 

$(".button").click(function() {
	
	$("#date").html("<p>Loading...</p>");
	
	$.get($(this).attr('href'), function(data) { 
		$("#data").html(data); 
	}); 
	
	return false; 

}); 

 

 

Link to comment
Share on other sites

Did you check your error log for a more comprehensive analysis?

Apart from the error:

$("#date").html("<p>Loading...</p>");

should probably refer to #data and your template code should probably only return the part "// some images", otherwise you'll nest a new section for each button click.

I case you intend to do more dynamic updates, returning a JSON instead of pure HTML may be an option:

 

  • Like 1
Link to comment
Share on other sites

i did, 500 error again!

send	@	jquery.js:2
ajax	@	jquery.js:2
w.(anonymous function)	@	jquery.js:2
(anonymous)	@	main.js:43
dispatch	@	jquery.js:2
y.handle	@	jquery.js:2

main.js:43

$.get($("this").attr('href'), function(data) { 

 

Link to comment
Share on other sites

4 hours ago, Autofahrn said:

Wondering if its not just:


$.get(this.attr('href'), function(data) { 

  

don't think so.

i have a follow up question. i get data as a whole. is there a way to get the content of specific fields. like data->divWithClassX?
i want to update the content of two different divs on the page.

do i have to that with the json-method?

Link to comment
Share on other sites

ok, somehow mixed up that this.attr, doesn't work...

To access individual fields and don't want to use JSON (any reason for that), you may inject that into your DOM tree and access relative to the parent element. That way you:

- combine discrete information into some markup
- deal with proper escapement of special chars etc.
- send that markup to client
- client needs to parse markup to retrieve discrete information (and decode handling of special chars)

Using the JSON version is much more straight forward, scalable and takes care of most aspects of proper data escapement.

  • Like 1
Link to comment
Share on other sites

6 minutes ago, Autofahrn said:

ok, somehow mixed up that this.attr, doesn't work...

To access individual fields and don't want to use JSON (any reason for that), you may inject that into your DOM tree and access relative to the parent element. That way you:

- combine discrete information into some markup
- deal with proper escapement of special chars etc.
- send that markup to client
- client needs to parse markup to retrieve discrete information (and decode handling of special chars)

Using the JSON version is much more straight forward, scalable and takes care of most aspects of proper data escapement.

reason is my shitty javascript skills. i'll give it a try ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...