PHP 回显问题

PHP echo problems

本文关键字:问题 PHP      更新时间:2023-09-26

我有一个函数,我的回声是:

echo '<tr>
<td align="center" style="padding:5px;"><img src="/chat/emotes/smile.png" onclick="insertSmiley("hallo")"> <br>:illuminati:</td>
               </tr>'

问题是onclick="insertSmiley("hallo")"必须与'在一起,而不是与"在一起。如果我把它放在 html 中,一切正常,但当我点击时,php echo 什么也没发生。

我的索引.php正文中有这个脚本:

<script type="text/javascript"> 
    function insertSmiley(smiley) 
    { 
        var currentText = document.getElementById("send"); 
        var smileyWithPadding = " " + smiley + " "; 
        currentText.value += smileyWithPadding; 
    currentText.focus(); 
    } 
</script> 

另一个代码在我的聊天中.php:

    echo '<textarea id="send" maxlength="125" rows="2" placeholder="Enter your message"></textarea>
<tr>
    <td align="center" style="padding:5px;"><img src="/chat/emotes/smile.png" onclick="insertSmiley("hallo")"> <br>:illuminati:</td>
                   </tr>

我真的认为问题是因为我不能在回声中使用'',我需要它来onClick...('hallo').

使用 '' 打印'

反斜杠在 PHP 中用于转义引号内的特殊字符。由于PHP不区分字符串和字符

如果你这样写

echo 'check it '' out'; 

它将给出这样的输出

check it ' out

所以像这样使用

echo '<tr>
<td align="center" style="padding:5px;"><img src="/chat/emotes/smile.png" onclick="insertSmiley(''hallo'')"> <br>:illuminati:</td>
               </tr>'

事实上,你没有回显echo语句中的任何变量,只需在代码之外添加 PHP 标签:

//Your PHP code here
?>
        <tr>
            <td align="center" style="padding:5px;">
                <img src="/chat/emotes/smile.png" onclick='insertSmiley(" hallo")'>
                <br>:illuminati:
            </td>
        </tr>
    <?php
// Continue your php code

如果您必须在 HTML 中回显变量,请按以下方式执行此操作:

$greetings = "Hallo";
    //Your PHP code here
    ?>
            <tr>
                <td align="center" style="padding:5px;">
                    <img src="/chat/emotes/smile.png" onclick="insertSmiley('<?php echo $greetings; ?>')">
                    <br>:illuminati:
                </td>
            </tr>
        <?php

试试这个:

 <?php
   echo "<textarea id="send" maxlength="125" rows="2" placeholder="Enter your message"></textarea>";
   echo "<tr>";
   echo "<td align="center" style="padding:5px;"><img src="/chat/emotes/smile.png" onclick="insertSmiley("hallo")"> <br>:illuminati:</td>";
   echo "</tr>";
?>

您可以使用:

onclick="insertSmiley(''hallo'')"