Solved

Easy: how do I echo javascript with php?

Posted on 2012-03-21
2
281 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
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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now