I'm using jQuery to hijack a link pointing to a php file. This php file is mostly html, but calls two php functions that each use a for loop to output html. The problem I'm running into is that the ajax call is incorrectly nesting the html generated by the php for loop.
Including the php file directly onto the page works fine, but when trying to grab it via ajax the html content of each sequential pass through the loop is inserted inside the previously generated html. For example:
<div>Content1</div>
<div>Content2</div>
<div>Content3</div>
becomes:
<div>Content1<div>Content2<div>Content3</div></div></div>
This is the relevant code for the php file:
<div class="publishedPosts">
<h3>Published</h3>
<?php displayPublishedBlogPosts(); ?>
</div>
<div class="verticalDivider"></div>
<div class="draftPosts">
<h3>Saved Drafts</h3>
<?php displayBlogPostDrafts(); ?>
</div>
And this is the jQuery code:
function adminPanelTabs() {
$('#adminPanelTabs ul li a').click(function(e) {
$('#adminPanelTabs ul li a.current').removeClass('current');
$(this).addClass('开发者_运维技巧current');
$('#adminPanelContent').load($(this).attr('href'));
e.preventDefault();
});
}
Would appreciate any input or suggestions on how to fix this.
Go figure - spend hours researching how to fix this, post here as a last resort, then I figure out what the problem is 30 minutes later.
For the record, the problem was in the php functions being called. They included sections where the closing div tag was enclosed in an if statement.
The if statement simply checked to see if the user was logged in, and even though this was true apparently making an ajax call in this way fails such an if statement, hence the closing div tag was never added.
I'd like to thank Nick for responding, as that helped trigger an idea about where the problem might be.
精彩评论