Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php生成html以在ajax调用后返回_Php_Jquery_Html_Json_Compression - Fatal编程技术网

如何使用php生成html以在ajax调用后返回

如何使用php生成html以在ajax调用后返回,php,jquery,html,json,compression,Php,Jquery,Html,Json,Compression,我使用ajax调用在页面上生成所有html,从不刷新页面。我的设置类似于 HTML:测试链接 JQUERY: $("#user_click_here").live("click", function(event){ console.log("click on test link"); ajax_function("action=return_some_html"); }); ajax函数调用php,我在这里创建html。我要做的是用php生成html。我试着用这个: PH

我使用ajax调用在页面上生成所有html,从不刷新页面。我的设置类似于

HTML:
测试链接

JQUERY:

$("#user_click_here").live("click", function(event){
    console.log("click on test link");
    ajax_function("action=return_some_html");   
});
ajax函数调用php,我在这里创建html。我要做的是用php生成html。我试着用这个:

PHP:

当然,我希望它尽可能地压缩,最好不要像这样的东西:
\n\t\t\t\t
来占用空间

编辑:


嗯。我不认为每个人都注意到这是一个ajax调用,我必须返回一个json元素。我无法编写常见的
html代码一些html

它看起来像是上一个herdeoc(这就是
不使用herdeoc的地方。它很乱,只应该在需要在变量中存储长字符串时使用。
尝试写出希望返回的HTML页面,就像它是普通HTML页面一样。在需要PHP输出(变量、数组值等)的地方,开始一个PHP标记,回显该值,然后结束PHP标记。这比尝试使用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>
另外,我建议使用表格而不是浮动div。如果使用表格,级别、武器和力量将更容易与它们的值对齐

$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;">'; 
$data = array("html" => $html);
        return json_encode( $data );
HTML;
$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();
<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>
ob_start("clean_linebreaks");
function clean_linebreaks($input) {
    return str_replace("\n", "", str_replace("\r", "", $input));
}