0

I was wondering how would I make it so that when a button is clicked in my form, it runs a php function that is on the same file?

I tried doing something like this:

<form> 
<input type=button name="button" value="Submit" onClick="hello()">
</form> 

<?php
function hello(){
  echo "Hello"
}
?>

But when the button is clicked, nothing happens, neither of my functions are called. How would I go about doing this?

jh314
  • 27,144
  • 16
  • 62
  • 82
ShadowViper
  • 365
  • 1
  • 5
  • 24

2 Answers2

0

PHP is a server side language.

You need a client side language "JavaScript".

If you need really php execution : You need ajax requests on a url (script.php).

Initerworker
  • 139
  • 1
  • 12
-1

This:

<?php
function hello(){
echo "Hello"
}
?>

exists on the remote server and the rest of HTML in your browser. Even though they are in the same file they don't see each other.

Use AJAX PHP to make PHP functions run from within HTML pages

Here is the link AJAX PHP

Marina Dunst
  • 859
  • 1
  • 6
  • 19