最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to write html code inside php? - Stack Overflow

programmeradmin4浏览0评论

I want to write some html code in php. In this html code I am calling a javascript function. but while it is calling the function there is a problem. I think the problem is quotes but I couldn't fix it.

This is the html code that I want to put inside php.

<table>
 <tr>
 <a href="javascript:chng('img');"><img src="s.png" name="img"></a> 
 </tr>
</table>

and this is my javascript code;

<script type="text/javascript">
img1 = "s.png";
img2 = "k.png";
function chng(c_img) {
if (document[c_img].src.indexOf(img1)!= -1) document[c_img].src = img2;
else document[c_img].src = img1;
} 

</script>

How can i write this html inside php code?

Thanks

I want to write some html code in php. In this html code I am calling a javascript function. but while it is calling the function there is a problem. I think the problem is quotes but I couldn't fix it.

This is the html code that I want to put inside php.

<table>
 <tr>
 <a href="javascript:chng('img');"><img src="s.png" name="img"></a> 
 </tr>
</table>

and this is my javascript code;

<script type="text/javascript">
img1 = "s.png";
img2 = "k.png";
function chng(c_img) {
if (document[c_img].src.indexOf(img1)!= -1) document[c_img].src = img2;
else document[c_img].src = img1;
} 

</script>

How can i write this html inside php code?

Thanks

Share Improve this question edited Apr 27, 2011 at 16:40 Belinda 1,2312 gold badges14 silver badges25 bronze badges asked Apr 27, 2011 at 12:25 omeromer 352 gold badges3 silver badges5 bronze badges 2
  • 2 A table with one row and no cells? A href with a javascript: URI? A link with no text content? An image with no alt text? An image with a name attribute? Globals in JS? Unintelligible abbreviations in function names? document.name_of_element? if without {}? I think half a dozen puppies just died. – Quentin Commented Apr 27, 2011 at 12:27
  • this code is just sample. this is not my real code. i just wanna learn how to do it. then i will put the answer in my real code. – omer Commented Apr 27, 2011 at 12:39
Add a ment  | 

6 Answers 6

Reset to default 9
<?php

// your php code

?>

<table>
 <tr>
 <a href="javascript:chng('img');"><img src="s.png" name="img"></a> 
 </tr>
</table>

<?php

// your php code

?>

<script type="text/javascript">
img1 = "s.png";
img2 = "k.png";
function chng(c_img) {
if (document[c_img].src.indexOf(img1)!= -1) document[c_img].src = img2;
else document[c_img].src = img1;
} 

</script>

<?php 

you also could wrap your code in heredoc, and echo it afterwards http://www.php/manual/de/language.types.string.php#language.types.string.syntax.heredoc

you could echo the html; eg

echo "<table>
 <tr>
 <a href=\"javascript:chng('img');\"><img src=\"s.png\" name=\"img\"></a> 
 </tr>
</table>
";

Just escape the double quotes with a backslash.

You can use PHP heredoc syntax:

var $js = <<<JS

// code

JS;

Escapes and echo "<html_code>" is a noob style.

Use scriptlets and the mand echo.

<?
  echo "<script type=\"text\/javascript\">"
?>

and so far. But pay attention of masking quotations signs and backslashes, etc.

Write html tags inside php code isn't nice. Read about templates to php as Smarty

http://www.smarty/

发布评论

评论列表(0)

  1. 暂无评论