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.

How do I achieve this in PHP?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Razor
  • 2,581
  • 6
  • 21
  • 27
  • 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
  • What have you tried so far? Where are you stuck? Printing JS code happens in the same way as printing HTML markup – Nico Haase Jul 20 '22 at 18:13

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,351
  • 8
  • 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
  • 387
  • 3
  • 12
0
<script>

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

</script>

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