2016-04-27 80 views
1

我有一个功能,我的回音是:PHP的回声问题

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中什么也没有发生。

我的index.php在体内有此脚本:

<script type="text/javascript"> 
    function insertSmiley(smiley) 
    { 

     var currentText = document.getElementById("send"); 

     var smileyWithPadding = " " + smiley + " "; 
     currentText.value += smileyWithPadding; 
    currentText.focus(); 

    } 
</script> 

和其它代码是我chat.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> 

我真的觉得现在的问题是,因为我不能使用'' in echo,我需要它onClick...('hallo')

+0

你错过了','在你的第一个'echo'年代末声明和第二个'echo'声明缺少'';'最后,也! –

+0

您在双引号内使用''onclick =“insertSmiley(\'hallo \')”''双引号错误'' – jcubic

+1

您正在生成错误的html:'onclick =“insertSmiley(”hallo“)”>'。 'hallo'的第一个'''引用终止了onclick属性,这意味着onclick中的JS代码是一个语法错误。 –

回答

3

使用\'只是打印'

在反斜杠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>' 
+0

工作谢谢!!! –

+0

需要等待8分钟才能接受答案我会等待并接受它谢谢!! –

+0

乐意帮忙:) –

0

尝试这个:

<?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>"; 

?> 
+0

只要看看语法高亮.. – chris85

1

,你是不是呼应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 
0

您可以使用:

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