0

I'm so new on Php and HTML. I have a problem about calling function. I think I've done what i read on other articles but it's obvious i miss something.

My file is "login.php". I try to call a function by onclick method but it doesn't work.

My button to call the function:

<button type="submit" class="btn btn-warning" style="margin-right: 10px;margin-top: inherit;" onclick="signin()">Sign In</button>

My function:

<script>

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

</script>

When I click to the button, on console it says:

login.php:63 Uncaught ReferenceError: hello is not defined

Or I tried to write function like this:

<?php 

function signin() { echo "hello"; }; 

?>

But now console says:

login.php:33 Uncaught ReferenceError: signin is not defined

I appreciate your helpful answers from now. Thank you!

Johnny
  • 459
  • 1
  • 4
  • 12

1 Answers1

0

On client side you cannot call php functions directly from event handler. You have to implement a JavaScript method which calls a server side php script. Here an example based on jQuery.

HTML

<button>Click</button>

JavaScript

<script type="text/javascript">
    $(function() {
        $('button').click(function() {
            $.get('your-php-file.php');
        });
    });
</script>

PHP (your-php-file.php)

<?php
function signin() {...}

signin();

Very simple and untested example. Only for demonstration.

u-nik
  • 488
  • 4
  • 12