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
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();
Don't use HEREDOC. It's messy, and should only be used when you need to store long strings in a variable. Try writing out the HTML page you expect to be returned as if it was a normal HTML page. Where you need PHP output (a variable, array value, etc.), begin a PHP tag, echo the value, and then end the PHP tag. This is much easier than trying to use heredoc.
<div class="box">
<h2><a href="#" id="remove"><?php echo $text[0]; ?></a> </h2>
<div class="block">
<div style="float:left; width: 35%; margin:5px; padding: 1em; background: white;">
<p>Username: <a id="username"><?php echo $_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><strong id="level">empty</b><br />Weapon blabla<br />2 - 5<br />3/6<br /></p>
</div>
</div>
If you're worried about line breaks (you shouldn't be), you can add an output handler that removes them.
ob_start("clean_linebreaks");
function clean_linebreaks($input) {
return str_replace("\n", "", str_replace("\r", "", $input));
}
Also, may I suggest using tables instead of floated divs. The level, weapon, and power will be easier to line up with their values if you use a table.
精彩评论