如何在php echo中添加javascript / html代码

时间:2014-09-07 13:32:30

标签: php html

我需要php echo中的html td类来实现javascript功能和脚本。 td类最初工作得很好但是当我将它插入到PHP代码中时它不再起作用了。任何想法如何使其工作?

这是我的代码:

<?php 
        endforeach;
        if($exist==1)
        {
            echo "
            <tr>
                <td>$date1</td>
                <td>$m_time</td>
                <td>$mx_time</td>
                <td>$total_hrs</td>
                <td class="tbl-save"><img onclick="save(this);" id="<?php echo $list->id ?>" class="icon-save" src="<?php echo base_url(); ?>images/save.png" width="15" height="15" title="Save"></td>
                <td class="tbl-edit"><i onclick="edit(this);" id="<?php echo $list->id ?>" class="icon-pencil"></td>                
            </tr>";
        }
        else
        {
        }?>

7 个答案:

答案 0 :(得分:2)

问题的根本原因是类名周围的双引号正在终止回显字符串。你需要用反斜杠来逃避它们。

例如:

echo "<td class="tbl-save">"

应该是:

echo "<td class=\"tbl-save\">"

或者你可以使用HEREDOC而不用担心逃避双引号。例如:

echo <<<END
<td class="tbl-save">
END;

您可以在此处阅读有关HEREDOC的更多信息:http://php.net/manual/en/function.echo.php

一般情况下,我会避免将HTML和PHP混合为最佳实践,因为它可能会使代码难以阅读,并且大多数IDE都无法在字符串中突出显示HTML。考虑使用模板替代方案,例如twing / smarty或MVC框架。 这篇维基百科文章列出了您可以选择的模板引擎: http://en.wikipedia.org/wiki/Comparison_of_web_template_engines

答案 1 :(得分:2)

发生错误是因为您在echo语句中使用了双引号,它终止了您的字符串文字。

echo "<td class="something">";
     ^          ^
     |          |
The string    PHP thinks that the
literal be-   string literal ends
gins here     here.

要在字符串文字中使用双引号,可以使用以下选项:

  • 使用反斜杠转义双引号:\"。由于前面的反斜杠,双引号将被解释为文字双引号。
  • 使用单引号分隔字符串:

    echo '<td class="something">';
    

但也有其他选择:

  • 不要将HTML代码放在PHP echo语句中,只能放入PHP变量:

    <td><?php echo $date1; ?></td>
    
  • 将PHP变量绑定到自定义模板系统并解析输出:

    <?php
    
    // Turn on output buffering. It will not send the output away,
    // but hold it for later use.
    ob_start();
    
    ?>
    
    <td>%date%</td>
    <td>%mtime%</td>
    
    <?php
    
    // Here we store the contents of the output buffer
    // into a variable.
    // The output buffer will contain this:
    // <td>%date%</td>
    // <td>%mtime%</td>
    $contents = ob_get_contents();
    
    // Now we need to replace some of our own custom
    // variables with the PHP variables.
    $search = array("%date%", "%mtime%");
    $replace = array($date, $m_time);
    $contents = str_replace($search, $replace, $contents);
    
    echo $contents;
    
    ?>
    

答案 2 :(得分:1)

你不能回声“...... class =”tbl-save“......”

它必须是回声“... class ='tbl-save'...”

答案 3 :(得分:1)

在除了开始和结束双qoutes之外的所有双引号之前使用转义斜杠()。

答案 4 :(得分:1)

您需要转义echo "";内的双引号或在其中使用单引号。或者,您可以将echo "";更改为echo '';。缺点是你不能在里面使用变量,因为它们不会被解析。您需要“中断”字符串才能使用变量echo 'use of variable '.$variable.' mkay';

此外没有必要执行<?php echo $variable; ?>,因为无论如何都要在双引号内解析变量并且之前没有关闭php标记。但我建议你为了便于阅读“打破”上面的字符串。

echo '
        <tr>
            <td>'.$date1.'</td>
            <td>'.$m_time.'</td>
            <td>'.$mx_time.'</td>
            <td>'.$total_hrs.'</td>
            <td class="tbl-save"><img onclick="save(this);" id="'.$list->id.'" class="icon-save" src="'.base_url().'images/save.png" width="15" height="15" title="Save"></td>
            <td class="tbl-edit"><i onclick="edit(this);" id="'.$list->id.'" class="icon-pencil"></td>                
        </tr>';

答案 5 :(得分:0)

无需使用嵌套的<?php ?>标记。在echo ''中使用单引号,在其中使用双引号(例如,id="id1"等等。)。尝试类似下面的内容

if($exist==1)
    {
        echo '
        <tr>
            <td>$date1</td>
            <td>$m_time</td>
            <td>$mx_time</td>
            <td>$total_hrs</td>
            <td class="tbl-save">
                <img onclick="save(this);" id="'.$list->id.'" class="icon-save" src="'.base_url().'images/save.png" width="15" height="15" title="Save">
            </td>
            <td class="tbl-edit"><i onclick="edit(this);" id="'.$list->id.'" class="icon-pencil"></td>
        </tr>';
    }

答案 6 :(得分:0)

语法高亮显示器会显示问题所在。确保您的字符串已正确转义并终止。

此外,php字符串中还有<?php个标记。

<?php 
        endforeach;
        if($exist==1)
        {
            echo "
            <tr>
                <td>$date1</td>
                <td>$m_time</td>
                <td>$mx_time</td>
                <td>$total_hrs</td>
                <td class=\"tbl-save\"><img onclick=\"save(this);\" id=\"{$list->id}\" class=\"icon-save\" src=\"".base_url()."images/save.png\" width=\"15\" height=\"15\" title=\"Save\"></td>
                <td class=\"tbl-edit\"><i onclick=\"edit(this);\" id=\"{$list->id}\" class=\"icon-pencil\"></td>                
            </tr>";
        }
        else
        {
        }?>