Michael34 Posted February 18, 2015 Share Posted February 18, 2015 <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 More sharing options...
Michael34 Posted February 18, 2015 Author Share Posted February 18, 2015 Any hint would be grealy appreciated Link to comment Share on other sites More sharing options...
justsomeguy Posted February 18, 2015 Share Posted February 18, 2015 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 More sharing options...
Michael34 Posted February 19, 2015 Author Share Posted February 19, 2015 Thank you justsomeguy i got a friend's help and now my php script is working fine. I still have so much to learn! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now