2012-11-19 53 views
0

我可以把php放在onfocus和onblur上吗?在下面的代码中,有没有我可以写的方法:Textarea onfocus,onblur和php

comment<?php echo $row['titol_post'] ?> 

在现在有评论的地方?

<?php $result = mysql_query("SELECT * FROM blog WHERE id= 1") or die(mysql_error()); 
while($row = mysql_fetch_array($result)) { ?> 

<Form id="comentari" method="POST" action="rebut.php"> 

    <textarea name="text_comment" rows="2" cols="30" 
     onfocus="if (this.value=='Comment') this.value = '';" 
     onblur="if (this.value=='') this.value = 'Comment';">Comment</textarea>  
    <input type="submit" class="boto" name="comentari" value="enviar"/> 

</Form> 

<?php } ?> 
+0

不要滥用'价值'为'

+0

@Quentin:最好的办法是什么? – Nrc

+0

'' – Quentin

回答

0

是的,你可以做到这一点。

<textarea name="text_comment" rows="2" cols="30" 
    onfocus="if (this.value=='Comment') this.value = '';" 
    onblur="if (this.value=='') this.value = 'Comment';">Comment<?php echo $row['titol_post'] ?></textarea>  
<input type="submit" class="boto" name="comentari" value="enviar"/> 

但是,您不能在onblur/onfocus的标签内做到这一点。也就是说,混合Javascript(客户端)和PHP(服务器端)。

+0

我可以在var中存储值<?php echo $ row ['titol_post']?>然后把它放在onbocus上,onblur? – Nrc

+0

不,PHP代码在服务器端的Javascript代码之前执行。而JS则在客户端执行。因此,除非您使用AJAX,否则PHP代码不能即时执行。但是,在这种情况下,这不是你想要做的。 –

0

这取决于PHP在html被发送到浏览器之前得到处理,所以如果它是一个固定值,是的,但如果值取决于在页面上选择的东西或例如javascript,那么不会。

+0

如何在html之前处理php? – Nrc

0

PHP在服务器上运行。它永远不会被客户看到,这意味着您可以让PHP在文档的任何位置执行输出。如果您正在编写onwhatever标签属性,或者只是输出纯文本或输出.jpg文件的原始二进制内容,那么PHP不会轻视。它对PHP无关紧要。