[EdLUG] Help (Again ...)
Jeremy Nicoll
jn.ml.elx.813 at letterboxes.org
Fri May 22 18:48:42 UTC 2020
On Fri, 22 May 2020, at 18:52, Andrew Ramage wrote:
> I have my website up and (mostly) correct. I just need to get the
> Directory to function correctly.
>
> andrewnramage.co.uk
>
> Copying and altering code from w3schools, I have this function. Why
> will it not work?
You're not showing us all the code.
None of us can see the php file(s) because if we try to look at
http://andrewnramage.co.uk\xyz.php
what we'd see is the result of executing that php code, not its
actual source.
When you say something does not work, that's the world's most
useless statement.
We have no idea what (if anything) does execute both in your browser
and on the server.
You could use your browser's developer tools to watch your javascript
code execute, and (I expect) somehow log what the php code does on
the server (though I don't know how that's done, especially if the server
in question is not yours). But I'm sure google will help.
> <script>
> // xhttp.onreadystatechange = function() {
> // if (this.readyState == 4 && this.status == 200) {
> myFunction(this);
> // }
In that set of lines 3/4 of them are commented out. I'm not
sure if the myFunction() bit is also not functioning because of
the commented-out bits.
If it is not being run and is not meant to be run, why did you include it
in the question?
If it is being run, where is the definition of myFunction() ? We are
not clairvoyant.
WHAT do you think it is mean to do?
> };
> xhttp.open("GET", "directory.xml", true);
> xhttp.send();
> }
Do those lines execute? I'm guessing they cause the
directory.xml file to be fetched, but is that right? Under what
circumstances does it happen? Before this page loads, or
asynchronously? If the latter how does any code that then
uses it know it's been fetched?
Skipping over the function dirmusic() definition for now
you also have
> <button margin-bottom=400px onclick=directory.php>Refresh</button>
I was under the impression that the onlick action of a button would be a
call of a JS function. But you seem to be referencing a php page on your
server.
If I'm right (that the action needs to be a JS function call) I can't imagine that
your code will do anything at all.
If it does somehow execute directory.php no-one here has a clue what is
in that file.
> <div id="dir">Directory goes here</div>
> <script>
> musicdir();
> </script>
What is musicdir() meant to do?
Where is the function defined?
You'll note it is NOT the thing defined here:
> function dirmusic() {
I have to ask... do you have any experience programming anything?
You're making loads of beginner-type mistakes, not the sort of
thing I'd expect to see an experienced programmer do.
Getting a mix of JS executed in a browser and PHP executed on a
server to work, if you don't CLEARLY understand the relationship
between them, and the limitations on what each can do, is going
to be hard work.
I think you're trying to be too clever. In your position I would set
up some much simpler pages - some eg just with JS on them, and
learn how to use your browser's dev tools, especially its JS
debugger ... so you can later watch whether your more complex
JS is doing what you expect.
I'd also learn how to debug php, with some simple examples on
your server. See eg:
https://blog.teamtreehouse.com/how-to-debug-in-php
or the simpler options described at
https://www.php.net/manual/en/debugger.php
where the idea of one is eg that when the php program needs to
show the programmer what the value of something is, it generates
some literal HTML containing the diagnostic message. So the
programmer sees that inside the generated web page that their
browser fetches.
Anothr example creates log file (which will of course be on the
server) and writes logging info to that. Whether you can do that
in your case might depend on what ther webhosting company
let you do (unless it's your own server, of course).
Ignore the suggesions to use various sophisticated php debuggers;
they're fine for professional programmers who're going to be doing
this every day... but they'll take ages to learn how to use. Stick to
simple methods where you can understand how they work.
I would not try to make the two sides (JS in a browser and php on a
server) co-operate until I knew how to do adequate debugging of
both parts..
It looks to me as if the intention of the code in the function is to
read the xml file - which seems to contain a list of song elements
inside a directory one... and is presumably generated elsewhere
and instead make the thing that generates the XML data generate
an html table in eg "mytable.htm"
Then the current page could be a .shtml one rather than .html and
you could embed the current copy of the most-recently generated
table with eg
#include virtual="/path/to/mytable.htm"
There are disadvantages to using shtml and server-side includes,
mainly that unless you run a server on your own PC you can't test
the result without uploading the shtml files to the wehost's server,
because it's the server that sees the "shtml" extension on a file and
preprocesses it to expand the embedded #include requests.
See: https://en.wikipedia.org/wiki/Server_Side_Includes
Note: I have never used php, nor JS. But I did program for a
living.
--
Jeremy Nicoll - my opinions are my own.
More information about the EdLUG
mailing list