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

  • SEARCH
  • Home
  • 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 7515919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:57:57+00:00 2026-05-30T00:57:57+00:00

The Application In PHP, I will output some initial views so web crawlers can

  • 0

The Application

In PHP, I will output some initial views so web crawlers can scrape my site:

 ----------------
| cool view 1    |
 ----------------
| cool view 2    |
 ----------------
| cool view 3    |
 ----------------
| Load more      |
 ----------------

Javascript also needs to dynamically generate the same views. When a user hits the “load more” button, it does an AJAX to the server and attaches the result to the page:

 ----------------
| cool view 1    |
 ----------------
| cool view 2    |
 ----------------
| cool view 3    |
 ----------------
| cool view 4    |
 ----------------
| cool view 5    |
 ----------------
| cool view 6    |
 ----------------
| Load more      |
 ----------------

Current Implementation

Currently, my PHP views and JS views are written separately. Here’s an example of loading a hypothetical PHP view using the CodeIgniter framework.

view.php

<?php
<div>
    <span>cool view</span>
    <span><?=$id?></span>
</div>
?>

controller.php

$this->load->view(
    'view', 
    array('id' => '999')
);

And here’s an example of the AJAX to load the hypothetical views with the Prototype framework.

ajax.php

echo json_encode(
    array('id' => random())
);

view.js

MyView = Class.create({
    initialize: function(id) {
        var div = new Element('div');
        var span0 = new Element('span').update('cool view');
        var span1 = new Element('span').update(id);
        div.appendChild(span0);
        div.appendChild(span1); 
        return div;
    }
});

controller.js

new Ajax.Request(
    '/index.php/someController/someMethod', {
        onSuccess: function(transport) {
            var response = transport.responseText.evalJSON();
            $('viewsContainer').appendChild(
                new MyView(response.id)
            )
        }
    }
);

Need a Cleaner Solution

I need a way to share the HTML template across PHP and JS without rewriting everything. In my actual project, I have dozens of views. Duplicating the view code in PHP and JS seems hard to maintain. I didn’t go with the cheap route of PHP printing out straight HTML to AJAX because 1) there’s extra size overhead to include the HTML tags, 2) it’s not client agnostic – that is, a non-browser client would not be able to understand the output, and 3) it doesn’t allow JS to cleanly attach listeners on subviews within the view.

  • 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-30T00:57:59+00:00Added an answer on May 30, 2026 at 12:57 am

    you could serve the final html, directly to the browser:

    if(<request is from ajax>)
       echo $this->load->view('view', array('id' => '999'), false);
    

    the final parameter false loads the view into a variable, in this case returns it…

    then, on the javascript side, you would just create a new div, and set it’s innerHTML to the response.text

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

Sidebar

Related Questions

I'm starting to develop a web application in PHP that I hope will become
I'm currently writing a web application in HTML5/Javascript and PHP. Some employees of the
I'm building a PHP web application that will run on my machine, and one
I have a PHP application that will on occasion have to handle URLs where
I am writing a PHP application that will have the ability to edit settings
I have a basic ajax application, which will not work, instead the php code
I'm writing a web application (PHP) for my friend and have decided to use
I'm developing a multilingual PHP web application, and I've got long(-ish) texts that I
if one web user is entering my php site and interacts with it then
I am trying to write an application that will use PHP as a scripting

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.