Jump to content

.php function not working

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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...