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 8826073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:01:59+00:00 2026-06-14T07:01:59+00:00

I’m working on my first Single Page Application, and I’m not sure the best

  • 0

I’m working on my first Single Page Application, and I’m not sure the best way to go about storing the page data.

I have it set up so that the page contents is saved in my database, and when a user clicks on one of the navigation links, it does an ajax call to a process file which extracts the page contents, formats it as json and echos it back so that the jQuery can populate the page.

The issue I’m having is that if any of the page contents in the database contains PHP, the PHP is not being processed or even displayed (however if I view source I see the raw php). Is their a better way to go about this so that the code will run?

My Ajax Call:

$("nav a").click(function(e) {
    e.preventDefault();
    var page = $(this).attr("href");
    $("nav a").removeClass("menuSelected");
    $(this).addClass("menuSelected");

    form_data = {
        action: "load",
        page: page,
        is_ajax: 1
    }

    $.ajax({
        type: "post",
        url: "process.php",
        dataType: "json",
        data: form_data,
        success: function(data) {
            if(data.success === "true") {
                $("#asideLeft").html(data.aside_left);
                $("#contentMiddle").html(data.content_middle);
                $("#asideRight").html(data.aside_right);
            } else {
                $("#contentMiddle").html("<h1>AJAX Failure</h1>");
            }
        }
    });

My PHP Code that get called when a user clicks on the link:

if($action == "load") {
    $pms->connect();
    $result = $pms->dbh->prepare("SELECT * FROM pages WHERE name=:name");
    $result->execute(array(':name' => $page));
    $page = $result->fetch(PDO::FETCH_OBJ);
    $data['success'] = "true";
    $data['aside_left'] = $page->aside_left;
    $data['content_middle'] = $page->content_middle;
    $data['aside_right'] = $page->aside_right;
    echo json_encode($data);
}

and a sample of the HTML/PHP that is being saved in the database:

<h1>Content Middle</h1>
<p>Today is <?php echo date("Y-m-d H:i:s"); ?> </p>

Thank you for any help or suggestions you can provide.

  • 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-06-14T07:02:00+00:00Added an answer on June 14, 2026 at 7:02 am

    +1 for the question. I agree that php code and the html (from the database) should be separated. In particular, while assembling the json data on the server side, you should invoke all the php code that you are now storing in the database. So, instead of storing in the database something like:

    <p>Today is <?php echo date("Y-m-d H:i:s"); ?> </p>
    

    You could write something like:

    $data['success'] = "true";
    $data['aside_left'] = $page->aside_left;
    $data['content_middle'] = $page->content_middle;
    $data['aside_right'] = "<p>Today is" . date("Y-m-d H:i:s") . "</p>";
    echo json_encode($data);
    

    Yes, this is not as clean as just retrieving data from the database, but all the server-side scripts should be executed on the server side. Problems like this one in particular – like printing today’s date and similar – can be solved using JavaScript, which can be stored in the database hassle-free (e.g. you store in the database a function that,once loaded on the server side, returns current date). This can be achieved if you store in the database a record like this one:

    "<p><script>(new Date()).toString('dddd, MMMM ,yyyy');</script></p>"
    

    Once loaded, it will display the current date inside the p tags.

    Another solution, if JavaScript simply isn’t an option and you have to use PHP, this could be achieved with another request to the server side, as such:

    .
    .
    $data['aside_right'] = "<p>Today is:<script> $.ajax({type:'post',url:'processSomeMore.php',dataType:'json',data:form_data,success:function(data) {...}});
    </script></p>";
    echo json_encode($data);
    

    Once the JavaScript code is sent to the client along with the rest of the html retrieved from the database, it will issue another request to the server, demanding for additional data through ajax. Hope this is not too confusing :-).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm making a simple page using Google Maps API 3. My first. One marker
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

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.