Jump to content

.php function not working


Michael34

Recommended Posts

<button onclick="myfunction()">Submit</button><?phpfunction myfunction(){ golbal $qual; if (document.getElementById('nota1').checked) { $qual++}; if (document.getElementById('nota2').checked) { $qual++}; if (document.getElementById('nota3').checked) { $qual++; echo "Your score is: $qual out of 30"}; else {echo "First check the answers you think correct"};}?>

 

I don't understand why this php snippet (which i have included in a html with a form in it) is wrong. When i click "Submit" button nothing happens.

Link to comment
Share on other sites

You're trying to use Javascript to execute a PHP function. That's not how either Javascript nor PHP works. That onclick handler can only execute Javascript code, not PHP code. If you want Javascript to interact with PHP then you need to send an ajax request to the server to execute some code and return the result. You also have Javascript code inside PHP. PHP does not have a document object, it does not have functions like document.getElementById, and it's not even valid to use . in PHP to refer to an object's properties and methods. That is Javascript, not PHP. Javascript and PHP are completely unrelated to each other, they do not share anything, not even an execution environment. Don't try and mix them like that, that's not how they work.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...