Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to generate html with php to return after ajax call
    text
    copied!<p>I generate all my html on my page with ajax calls and never refresh my page. My setup is something like </p> <p>HTML: <code>&lt;a id="user_click_here"&gt;test link&lt;/a&gt;</code></p> <p>JQUERY:</p> <pre><code>$("#user_click_here").live("click", function(event){ console.log("click on test link"); ajax_function("action=return_some_html"); }); </code></pre> <p>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:</p> <p>PHP: </p> <pre><code> $html = &lt;&lt;&lt;HTML &lt;div class="box"&gt; &lt;h2&gt; &lt;a href="#" id="remove"&gt;$text[0]&lt;/a&gt; &lt;/h2&gt; &lt;div class="block"&gt; &lt;div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;"&gt; HTML; $html .= '&lt;p&gt;Username: &lt;a id="username"&gt;' . $_SESSION['username'] . '&lt;/a&gt;&lt;/p&gt;'; $html .= &lt;&lt;&lt;HTML &lt;div style="float:left; width: 30%; margin:5px;"&gt; &lt;p&gt; Level:&lt;br /&gt; Weapon:&lt;br /&gt; Power:&lt;br /&gt; Bullets:&lt;br /&gt; &lt;/p&gt; &lt;/div&gt; &lt;div style="float:right; width: 60%; margin:5px;"&gt; &lt;p&gt; HTML; $html .= '&lt;b id="level"&gt;empty&lt;/b&gt;&lt;br/&gt;'; $html .= &lt;&lt;&lt;HTML &lt;---ERROR HERE unexpected '&lt;&lt;' Weapon blabla&lt;br /&gt; 2 - 5&lt;br /&gt; 3/6&lt;br /&gt; &lt;/p&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>I tend to just try and fail until it works with this <code>&lt;&lt;&lt;WHATEVER</code> (dont remember what it's called). Like now where I get an <code>unexpected '&lt;&lt;'</code> error for some reason.</p> <p>Do I have to use this method:</p> <pre><code>$html = '&lt;div class="box"&gt; &lt;h2&gt; &lt;a href="#" id="remove"&gt;' . $text[0] . '&lt;/a&gt; &lt;/h2&gt; &lt;div class="block"&gt; &lt;div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;"&gt;'; </code></pre> <p>?</p> <p>What is the best way to save html in php and send it back to jquery. </p> <p>I send the html like this now: </p> <p>PHP:</p> <pre><code>$data = array("html" =&gt; $html); return json_encode( $data ); </code></pre> <p>Off course I want it compressed as much as possible, preferably without any stuff like this: <code>\n\t\t\t\t</code> to take up space. </p> <p><em><strong>EDIT:</em></strong></p> <p>Ok. I don't think everybody noticed the fact that this is an ajax call and I have to <strong>return a json element</strong>. I cannot do the common <code>&lt;?php php code ?&gt; html code &lt;?php more php ?&gt; some html</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload