473,748 Members | 5,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Beginner: Javascript function call within URL from use of XMLHTTPRequest

1 New Member
The title probably isn't very clear, but I haven't been able to find this problem (or I must be having problems figuring out which search strings to use) so I apologize if this has been addressed before, and I apologize if this is the wrong forum since I'm trying to use the XMLHTTPRequest with Javascript and PHP and I couldn't figure out which category my problem belonged to.

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
  1. <script type="text/javascript" language="Javascript">
  2. <!--
  3.  var req = false;
  4.    try {
  5.      req = new XMLHttpRequest();
  6.    } catch (trymicrosoft) {
  7.      try {
  8.        req = new ActiveXObject("Msxml2.XMLHTTP");
  9.      } catch (othermicrosoft) {
  10.        try {
  11.          req = new ActiveXObject("Microsoft.XMLHTTP");
  12.        } catch (failed) {
  13.          req = false;
  14.        }  
  15.      }
  16.    }
  17.  
  18.    if (!req)
  19.      alert("Error initializing XMLHttpRequest!");
  20.  
  21. function sendRequest(id, program) {
  22.   req.open('get', 'programs?id=' + id + '&x=' + program);
  23.   req.onreadystatechange = handleResponse;
  24.   req.send(null);
  25. }
  26.  
  27. function handleResponse() {
  28.  
  29.   if(req.readyState == 4){
  30.     var response = req.responseText;
  31.     var update = new Array();
  32.  
  33.     if(response.indexOf('||' != -1)) {
  34.       update = response.split('||');
  35.       document.getElementById(update[0]).innerHTML = update[1];
  36.     }
  37.   }
  38.   else
  39.   alert("loading" + ajax.readyState);
  40. }
  41.  
  42. -->
  43. </script>
  44.  
  45. </head>
  46.  
  47. <body>
  48.  
  49. <a href="javascript:sendRequest('show_music', '<? echo $x ?>');">Music</a> |
  50. <div id="info">
  51.  
  52. </div>
  53.  
  54. </body>
  55. </html>
  56.  
programs.php:
Expand|Select|Wrap|Line Numbers
  1.  
  2. switch($_REQUEST['id']) {
  3.  
  4. case 'show_music':
  5.          echo "info||";
  6.          $program = $_REQUEST['x'];
  7.          query_music_song($program, $db);
  8.          mysql_close();
  9.          break;
  10. }
  11.  
  12. <script language="JavaScript">
  13.  
  14. function show(click_lyrics) {
  15.     if (click_lyrics.style.display == "none") {
  16.         click_lyrics.style.display = "";
  17.     } else if (click_lyrics.style.display == "") {
  18.         click_lyrics.style.display = "none";
  19.     }
  20. }
  21.  
  22. </script>
  23. ...
  24. <snip>
  25. ...
  26. echo "<a href=\"#\" onclick=\"show(lyrics)\">Show Lyrics</a><br />";
  27. echo "<span id=\"lyrics\" style=\"display:none\"><pre>" . $m->lyrics . "</pre></span>";
  28.  
  29.  
This is in addition to the use of php to query and output code that I took out.

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.
Oct 24 '06 #1
1 7706
acoder
16,027 Recognized Expert Moderator MVP
To run JavaScript returned from an Ajax request, you either have to eval it, or better still, add the script to the head of the main page:
Expand|Select|Wrap|Line Numbers
  1. var script = document.createElement("script");
  2. script.appendChild(document.createTextNode(code)); //code is a line of code
  3. var head = document.getElementsByTagName("head")[0];
  4. head.appendChild(script);
May 25 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2255
by: malcster2 | last post by:
hello, i am a beginner to ajax. i have created a mysql database, which i would like to access from a web page. i have created 3 files, a html to display the data, a php file to extract the data, and a javascript file to to the clever stuff. when i access the html page, all the data is displayed correcty, but if i add or delete any records, and press the button i have created to refresh the data(without reloading), the data doesn't change....
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.