I have a folder 'js' with the jQuery function and the PHP script. In another folder I have the HTML file. So this is the structure by now:
/server/js/global.js
/server/js/script.php
/server/html/student.html
The JavaScript code is called from HTML and its working fine, but the jQuery post function doesn't call the PHP script.
If I move the HTML file into the 'js' folder it works perfectly and the PHP script is called fine.
Why does this happens?
I mean: What is the path represented on the next path?
$.post('script.php',...);
Is it calling a file from the JavaScript folder or from the HTML folder?
The problem might be that the Javascript file global.js you are calling is using the path that all Javascript files use, which is the one relative to the webpage you are viewing at that moment. Check to see if this is the problem (like a relative path) and make sure the path is absolute instead of relative. So for example in the global.js the part that says:
$.post(
'script.php',
{ nameValue: name.value },
change it to:
$.post(
'/path/to/file/script.php',
{ nameValue: name.value },
As an example it will look like this for your case:
$.post(
'/server/js/script.php',
{ nameValue: name.value },
This way it will work for in any case that calls global.js out since the absolute path will not depend on where the viewing file is. It also adapts to any place you put your html file so there is no need to edit the file in the future (Assuming the absolute path will always be in the same place).
It depends how you're calling the JS code from your HTML file, but try using JQuery post like this:
$.post('../js/script.php', function(data) { } );