0

I have just started learning how to create web applications in PHP after migrating from ASP.NET.

I wanted to know how to integrate JavaScript with PHP. For example, there is a Home.php page that resides in the website's root folder. The page has an HTML button on it and a simple JavaScript function that contains code for a pop-up window that says, "Hello!" I want to call this function at the click event of this HTML button. Can anyone please help me? I know how this can be achieved in ASP.NET. I want to know how it can be done in PHP. After getting the solution to this problem, I will learn move to AJAX and then jQuery.

I am not aware whether a similar question like this has been posted on this forum before. If it has then the mods can take a call and delete this post and send me a PM containing the link to the similar question. :D

Thank you in advance.


Thank you all very much for your replies. :)

The solution that you have given me is in HTML; but how do I achieve this in PHP?

Razor
  • 2,521
  • 6
  • 19
  • 26
  • Check http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php – SalGad Dec 09 '13 at 07:17
  • you can directly write html and javascript code in PHP files as you do in html files, Apache firstly parse PHP file and create equivalent html and respond to browser and then browser will interpret that HTML. So you can use onclick="your_Function()" in your PHP file. – Jitendra Khatri Dec 09 '13 at 07:19
  • Okay now I got it. Thank you very much. :-) – Razor Dec 09 '13 at 07:48

3 Answers3

1

JavaScript which normally goes in head section.

<script type="text/javascript">
      function yourFunction() {

             alert("Helloc click ");
       }

    </script>

Your HTML

  <input onclick="yourFunction(); " value="value">

To start learning basic JavaScript a tutorial available here .

Noor
  • 1,345
  • 7
  • 27
0

Do it like this?

<input id="mybutton" type="button" target="_self" onclick="functionname(), return false;" name="mybutton" value="Click me!" />
Fex del Sollo
  • 366
  • 2
  • 12
0
<script>

function myFunction() {
  alert("Clicked");
}

</script>

<button onclick="myFunction()">Click me</button>
Gabriel C. Troia
  • 2,868
  • 2
  • 15
  • 16