0

I am working on site for users to do social netwoking. like facebook. or twitter. So far i have this code and let me show you what I want to do.

 <button id="welcome">Welcome user</button>
 <?php
     function dal(){
         alert("Hello user, welcome to my big social network")
     }
 ?>

and i do javascript like this

 <script>
     document.byID('welcome').onclick = dal();
 </script>

code currently is not working.

if you have any suggestion i really appreciate.

1
  • You will need to run through a basic tutorial. There is no byID in JS and you cannot dump a piece of JS in a php statement and expect it to be available to the client. Good luck with your Facebook killer Commented Jul 25, 2012 at 8:53

5 Answers 5

3

Remove the php and write this inside script tag

<script> 
window.addEventListener("DOMContentLoaded",
  function(){ 
    document.getElementById('welcome').onclick=dal;
  },false);
function dal(){
  alert("Hello user, welcome to my big social netowkr")
}
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Likely better executed onload
Hmm, what's wrong with .onload? - I formatted your code a bit. It was not very readable
because i don't know to which element i should add .onload
1

You can't call php functions in javascript.

1 Comment

That is not an answer, but a comment
1
 <button id="welcome" onclick="dal()">Welcome user</button>

<script type="text/javascript">
  function dal()
  {
    alert("Hello user, welcome to my big social network")
  }
</script>

Comments

1

This question really needs rewording. I honestly don't know where to start correcting the mistakes with this.... however to make the alert work for your button you need the following:

<button id="welcome">Welcome user</button>


<script>
  document.getElementbyID('welcome').onclick = function(){
   alert("Hello user, welcome to my big social network");
  }

 </script>

also you can't call or access PHP in JavaScript.

Please note the spelling mistake in your alert message too

Comments

1

It is considered best practice to unobtrusively add the handlers in the head instead of inline

I suggest

<?php ... ?>
<html>
<head>
<script>
window.onload=function() {
  document.getElementById('welcome').onclick=function() {
     alert("Hello user, welcome to my big social network")
  }
}
</script>
</head>
<body>
<div id="content">
  <button id="welcome">Welcome user</button>
</div>
</body>
</html>

2 Comments

but window.onload will run after every image and other files get loaded
That has never ever been a problem for me. But if that is too late, then your suggestion works in most modern browsers. Mine works in all browsers since document.getElementById was introduced

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.