Solved

Easy: how do I echo javascript with php?

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

760 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

17 Experts available now in Live!

Get 1:1 Help Now