Solved

Easy: how do I echo javascript with php?

Posted on 2012-03-21
2
292 Views
Last Modified: 2012-03-26
Why isn't this working?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<script type="text/javascript">
function moo() {
      alert("mooooo");
}

</body>

<p <?php echo "onclick=moo()" ?>>ljlkjljljkjlkj</p>

</html>
0
Comment
Question by:NewWebDesigner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 350 total points
ID: 37750754
you put your p tag outside body and there's no closing script tag

corrected :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<script type="text/javascript">
function moo() {
     alert("mooooo");
}
</script>
<p <?php echo "onclick='moo()'" ?>>ljlkjljljkjlkj</p>
</body>



</html>

Open in new window

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 37750881
There may be some assumptions about the interrelationship of PHP and the client browser.  PHP runs before the client ever gets control.  But there is also this:
<p <?php echo "onclick=moo()" ?>>ljlkjljljkjlkj</p>

Open in new window

Have you looked at the HTML with the W3C validator?  Is the opening paragraph tag correctly coded with the closing wicket?  Is there some advantage to using PHP here?  I cannot see it but maybe you can help us understand why you are doing it this way.
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question