Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6730037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:21:38+00:00 2026-05-26T10:21:38+00:00

I’m a bit swamped here. I got a project with rather dynamic content, so

  • 0

I’m a bit swamped here. I got a project with rather dynamic content, so it’s written in php. Depending on the user input, different files get loaded. So far no problem, but I also want to use ajax (jquery) to load the content (if user has js on) and that’s where I’m stuck a little.

For my php site structure I have something like this:

switch ($input) {
    case 'foo': require 'foo.php';
    break;

    case 'bar': require 'bar.php';
    break;
}

and in the actual content area some functions like

<div id="content"><php echo content_function(); ?></div>
<div id="sidebar"><php echo sidebar_function(); ?></div>

Those functions are defined both in bar.php and foo.php. That way I don’t have to struggle with different switch/if-else statements in the middle of my html and the whole file-handling is done in the switch statement.

When loading with jquery now I apparently can’t re-instantiate the functions in content and sidebar without reloading the page which would make the ajax load useless. So I added some extra code to foo.php and bar.php to be able to instantiate each function seperatly into the appropriate divs (after emptying them beforehand for sure). E.g.

if(isset($_POST) && $_POST['load_content_func']) { echo content_function(); }
if(isset($_POST) && $_POST['load_sidebar_func']) { echo sidebar_function(); }

Then I just take the output of each ajax request and load it into content and sidebar:

var foo_or_bar; //depending on user input either foo or bar
if (foo_or_bar == 'foo') {

    $.ajax({  
      type: "POST",  
      url: "foo.php",  
      data: load_content_func,  
      success: function(html) {  
        $('#content').html(html);

    $.ajax({  
      type: "POST",  
      url: "foo.php",  
      data: load_sidebar_func,  
      success: function(html) {  
        $('#content').html(html);
}

Note: All of the code is just an example and written out of my head, doesn’t matter if somethings wrong as long as you get the idea what I want to do!

Somehow it doesn’t feel right, but being the odd perfectionist, I’m never really happy, so it might be just me.
Is there a good practice for doing something like this and how do you usually solve this ? All suggestions welcome!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:21:39+00:00Added an answer on May 26, 2026 at 10:21 am

    Here’s the thing, and I hope it’s not aggravating due to it being a generalized response and posing more questions than answers:

    The way you propose is that there is the potential for new content to be loaded depending on user interaction, and when that new content is loaded, a new sidebar is also loaded. I have some thoughts on this, and a caveat which is: I’m not the world’s most experienced PHP developer. That said, here are my initial thoughts (they do not all relate to one-another):

    A) The reasons people use Ajax vary: to minimize the data being retrieved, to avoid the visual “flash” from reloading a whole page, and I’m sure there are other reasons. To decide if Ajax is even the tool for the job, you need to decide “why” you are using it.

    B) If you ALWAYS retrieve a “foo” sidebar to go with “foo” content, why not just fetch them at once? No matter what your PHP logic is on the server side, it can return with one request:

    <div id="content">.. all my content...</div>
    <div id="sidebar">.. my sidebar ...</div>
    

    There are very few well-structured pages out there that will have any markup separating the content from the sidebar. Yours might be one, though; I don’t know that for sure.

    C) If you ALWAYS retrieve “foo” sidebar to go with “foo” content, and you CAN’T return them in ordered markup (as above suggestion) you can still just ask for foo.php once, and in the success function you can parse html and send it to your two separate areas.

    D) If you tightly couple your “foo” sidebars and content like that, you will probably need to re-engineer it later. What if you decide that both “foo” and “bar” content areas can actually share “baz” sidebar? Your proposed way will mean that you’ll need to have logic checking for “baz” content to go with “baz” sidebar.

    E) If you don’t like my suggestion to just get both content and sidebar at once, I see nothing terribly wrong with doing two separate and distinct Ajax calls, though. Some people will come up with clever ways to do it with an iterator so that you only need to specify “foo.php” once (even though two calls are being made) and it will automagically grab both. But meh. Why make code esoteric and hard to read when two separate and distinct calls make it very clear what the intended purpose is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.