I'm starting to learn some very basic javascript for this but don't know how to mix JS with php - i.e. how to compare a php variable with a javascript variable.
It's not very helpful to think of "PHP variables" and "JS variables". There's just code and data, and there isn't always a difference between the two. It can be helpful to think of
host and embedded languages when dealing with documents that mix languages.
StackOverflow has many questions on how to pass data from
PHP to JS and
JS to PHP; start there. The gist is that you pass data between client and server the way you always do: in requests. You just have to decide on the format, and convert the data to that format for the request. PHP or whatever server-side technology you're using can generate resources of any type: HTML, JS, JSON, XML, CSS, images... You can simply have a PHP script generate the JS, either embedded or external. In the latter case, give the script an extension of ".js.php" and use some form of clean URLs, and the client doesn't even have to be aware that the JS is generated. Just make sure you set the
Content-type header properly, and pass any data (including strings) through
json_encode before outputting:
PHP:
<?php
header('Content-type: application/javascript');
$img = array('w' => 230, 'h' => 170);
?>
var img = <?php echo json_encode($img); ?>;
/* img == {w: 230, h: 170} */
Going from client to server, you can send a GET request and use query parameters, or POST (or PUT) the data and format it as
x-www-form-urlencoded, JSON, XML or some other format (JSON is a nice mix of simple & powerful). You can use a form to send the request, or a link (limited to GET requests; use JS to generate the query string), or AJAX techniques (
XMLHttpRequest or a hidden IFRAME), perhaps encapsulated in a JS library.
More on XMLHttpRequest:
This is where it's doing my head in because I assume they cannot compare due to the fact that one is server-side and one is client side. In other words, a database variable, echo'd with php is presumably parsed in Apache before javascript even starts working.
It depends on where in the document it's output. Note that Apache doesn't parse anything; it merely handles requests, possibly passing them off to a module or external process. PHP parses PHP, browsers parse HTML (and CSS) and hand off JS to their built-in JS engine.
Is this where the AJAX comes in?
Sort of. AJAX (Asynchronous JS And XML) lets you send a request without (re-)loading a page. You use JS to send the request. Originally, the response would be XML, but these days HTML and JSON are more common. The "asynchronous" is because the JS function call is, well, asynchronous–it returns before the (remote) computation is complete.
You don't need to use AJAX to submit data to a PHP script; you can simply submit as normal and let the page reload. AJAX can break the browser model (history and bookmarks, for example), so it should be used only when the browser model doesn't apply or when you can fix the break.