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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:56:36+00:00 2026-05-24T04:56:36+00:00

I generate all my html on my page with ajax calls and never refresh

  • 0

I generate all my html on my page with ajax calls and never refresh my page. My setup is something like

HTML: <a id="user_click_here">test link</a>

JQUERY:

$("#user_click_here").live("click", function(event){
    console.log("click on test link");
    ajax_function("action=return_some_html");   
});

The ajax function calls php, where I create the html. What I struggle with is to generate html with php. I try to use this:

PHP:

    $html = <<<HTML

    <div class="box">
        <h2>
            <a href="#" id="remove">$text[0]</a>
        </h2>
        <div class="block">

        <div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">

HTML;

    $html .= '<p>Username: <a id="username">' . $_SESSION['username'] . '</a></p>';     


    $html .= <<<HTML


            <div style="float:left; width: 30%; margin:5px;">
                <p>
                    Level:<br />
                    Weapon:<br />
                    Power:<br />
                    Bullets:<br />
                </p>
            </div>
            <div style="float:right; width: 60%; margin:5px;">
                <p>

HTML;

    $html .= '<b id="level">empty</b><br/>';    


    $html .= <<<HTML        <---ERROR HERE unexpected '<<'  



                    Weapon blabla<br />
                    2 - 5<br />
                    3/6<br />
                </p>
            </div>
        </div>

I tend to just try and fail until it works with this <<<WHATEVER (dont remember what it’s called). Like now where I get an unexpected '<<' error for some reason.

Do I have to use this method:

$html = '<div class="box">
        <h2>
            <a href="#" id="remove">' . $text[0] . '</a>
        </h2>
        <div class="block">

        <div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">'; 

?

What is the best way to save html in php and send it back to jquery.

I send the html like this now:

PHP:

$data = array("html" => $html);
        return json_encode( $data );

Off course I want it compressed as much as possible, preferably without any stuff like this: \n\t\t\t\t to take up space.

EDIT:

Ok. I don’t think everybody noticed the fact that this is an ajax call and I have to return a json element. I cannot do the common <?php php code ?> html code <?php more php ?> some html

  • 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-24T04:56:37+00:00Added an answer on May 24, 2026 at 4:56 am

    It looks like your last HEREDOC (that’s what the <<< syntax is called) is unclosed, missing it’s

    HTML;
    

    And don’t forget that the final HTML; cannot have any whitespace on the same line before or after it.

    However, you’re going about it all wrong. The great thing about the HEREDOC syntax is that you can embed all your variables into it without requiring any concatenation. Just create the whole thing in one HEREDOC and echo it out to jQuery. There’s no need to make it JSON if you are just going to use it as HTML when received by the AJAX call.

    All your code above belongs inside one $html = <<<HTML block. Enclose all your complex variables like $_SESSION['whatever'] in {} {$_SESSION['whatever']}.

    $html = <<<HTML
    
    <div class="box">
        <h2>
            <a href="#" id="remove">{$text[0]}</a>
        </h2>
        <div class="block">
    
        <div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">
          <p>Username: <a id="username"> {$_SESSION['username']}</a></p>
            <div style="float:left; width: 30%; margin:5px;">
                <p>
                    Level:<br />
                    Weapon:<br />
                    Power:<br />
                    Bullets:<br />
                </p>
            </div>
            <div style="float:right; width: 60%; margin:5px;">
                <p>
                 <b id="level">empty</b><br/>
         <!--etc -->
         <!--etc -->
    HTML;
    
    // Now just echo it back to the AJAX caller as HTML
    echo $html;
    exit();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app uses javascript to generate all the HTML for my website. The index.html
I am making a jQuery Ajax form submit to a PHP page that I
I was using jQuery.load() to load a html page's content into a lightbox. The
I currently have a PrintingWebService that I call from an AJAX page with all
I have docbook generated HTML that contains small, fixed size images. All these images
How do I generate all the permutations of a list? For example: permutations([]) []
Algorithm to generate all possible letter combinations of given string down to 2 letters
I know you can generate all permutations from a list, using glob or Algorithm::Permute
I need to generate all combinations (not permutations) in VB .NET, I've been search
Possible Duplicate: How to generate all permutations of a list in Python I want

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.