I am completely new to AJAX and rusty with Javascript. I created a XMLHTTPRequest object on "index.php" that calls to another page 'programs.php' that queries and outputs music information for a skating program.
In the output of 'programs.php' it contains:
index.php:
Expand|Select|Wrap|Line Numbers
- <script type="text/javascript" language="Javascript">
- <!--
- var req = false;
- try {
- req = new XMLHttpRequest();
- } catch (trymicrosoft) {
- try {
- req = new ActiveXObject("Msxml2.XMLHTTP");
- } catch (othermicrosoft) {
- try {
- req = new ActiveXObject("Microsoft.XMLHTTP");
- } catch (failed) {
- req = false;
- }
- }
- }
- if (!req)
- alert("Error initializing XMLHttpRequest!");
- function sendRequest(id, program) {
- req.open('get', 'programs?id=' + id + '&x=' + program);
- req.onreadystatechange = handleResponse;
- req.send(null);
- }
- function handleResponse() {
- if(req.readyState == 4){
- var response = req.responseText;
- var update = new Array();
- if(response.indexOf('||' != -1)) {
- update = response.split('||');
- document.getElementById(update[0]).innerHTML = update[1];
- }
- }
- else
- alert("loading" + ajax.readyState);
- }
- -->
- </script>
- </head>
- <body>
- <a href="javascript:sendRequest('show_music', '<? echo $x ?>');">Music</a> |
- <div id="info">
- </div>
- </body>
- </html>
Expand|Select|Wrap|Line Numbers
- switch($_REQUEST['id']) {
- case 'show_music':
- echo "info||";
- $program = $_REQUEST['x'];
- query_music_song($program, $db);
- mysql_close();
- break;
- }
- <script language="JavaScript">
- function show(click_lyrics) {
- if (click_lyrics.style.display == "none") {
- click_lyrics.style.display = "";
- } else if (click_lyrics.style.display == "") {
- click_lyrics.style.display = "none";
- }
- }
- </script>
- ...
- <snip>
- ...
- echo "<a href=\"#\" onclick=\"show(lyrics)\">Show Lyrics</a><br />";
- echo "<span id=\"lyrics\" style=\"display:none\"><pre>" . $m->lyrics . "</pre></span>";
My problem is that the function call to show(click_lyri cs) does not seem to work, which I suppose is to be expected. So my question is whether there is a way to make this work in hiding the lyrics or show the lyrics within the requested data? Or perhaps a better way to code this? Let me know if more code/explanation is needed for clarity.
Thanks much in advance.