SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: AJAX problem

Hybrid View

  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX problem

    Hi,

    This isnt strictly a php question as all the php involved works fine but i figured the php forum would be the best to post this question in as opposed to the javascript forum. If theres somewhere more appropriate for this Mr Moderator then please move it.

    I have a script here:

    Code JavaScript:
    <html>
    <head>
    <title>test page</title>
    <script type="text/javascript" src="text-utils.js"></script>
    <script language="javascript" type="text/javascript">
     
     
       var request = null;
     
       function createRequest() {
         try {
           request = new XMLHttpRequest();
         } catch (trymicrosoft) {
           try {
             request = new ActiveXObject("Msxml2.XMLHTTP");
           } catch (othermicrosoft) {
             try {
               request = new ActiveXObject("Microsoft.XMLHTTP");
             } catch (failed) {
               request = null;
             }
           }
         }
     
     
         if (request == null)
           alert("Error creating request object!");
       }
     
       function get_message() {
       	var message_id = document.getElementById("message_id").value;
        createRequest();
       	var url = "edit_message.php?message_id=" + escape(message_id);
       	request.open("GET", url, true);
       	request.onreadystatechange = open_message;
       	request.send(null);
     
       }
     
       function open_message() {
       	if(request.readystate == 4) {
       		var message_txt = request.responseText;
       		var message_area = document.getElementById("message_text");
       		replaceText(message_area, message_txt);
     
       	}
       }
     
    </script>
    </head>
    <body>
     <table style="float: left; width: 30px; background-color: red;">
    <?php
    require_once('../mysql_connect.php');
    $sql = "SELECT * FROM message ORDER BY message_id";
    $query = mysql_query($sql);
    while($row = mysql_fetch_assoc($query)) {
      $message = $row['message'];
      $author = $row['author'];
      $message_id = $row['message_id'];
      ?>
     
      	<tr>
      		<td><?php echo $message; ?></td><td><span style="color: red; font-weight: bold;"><?php echo $author; ?></span></td><td>
      		<form method="GET">
      		<input type="hidden" id="message_id" value="<?php echo $message_id; ?>">
      		<input type="button" value="edit" onClick="get_message();" /></td> </form>
      	</tr>
     
     
     
     
      <?php 
     
    }
     
    ?>
      </table>
      <div style="float: left; width: 400px; margin-left: 20px; height: 400px; background-color: blue;" id="textbox">
      <p id="message_text">some text</p>
      	</div>
     
     
    </body>
    </html>



    Now all it should do is go off and get the message from the server and put the text back in the textbox div. Now the edit_message.php script works fine on itw own and returns what it should. Ive also looked at the source and the right value is in the hidden input.

    Problem is when you click on the edit button nothing happens!

    can anyone spot my problemo??

    thanks
    Last edited by elduderino; Jun 10, 2007 at 13:11.

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should check the HTTP Status Code too in the function open_message().

    Code JavaScript:
    if(request.readyState == 4 && request.status == 200) { ...

    The rest seems to be ok, so my first guess would be to check the replaceText function...

    Keep me posted.
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI,

    thanks for the reply.....it turned out to be something really stupid....i want using capital letters for request.readystate == 4......it should have been request.readyState == 4 !

    Now it works but not in the way i want it to! I have a list of messages with an edit button next to them....what i want to happen is when you click the edit button it loads the corresponding message in to the panel on the right....all thats happening now is the first message in the list gets loaded no matter what message edit button you click on.

    I cant think of a way to get it working properly

    i know the problem is here:

    var message_id = document.getElementById("message_id").value;

    because its just not making any distinction between which button is pressed and just getting the first message out....i just cant think of a way to get it to recognise which buttons been pressed

    Any ideas anyone?

    Thanks

  4. #4
    SitePoint Member
    Join Date
    Jun 2007
    Location
    Chicago
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try changing the form to recognize the message_id once its click.

    Are you able to send the parameters <url + escape(message_id)> to your server page successfully?

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    thanks for the reply and Welcome to sitepoint!!

    Yes i can send message_id to the server fine and the whole app 'works' so to speak but thats not the problem of course....the problem is getting the form to realise which button has been pressed.
    I think i have a solution though. I was going to add a onclickeventhandlers function thats set to run when the body loads (or whatever they are) and then use onclick this in my get_message function
    to pick up which button has been pressed......do u think this will work.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •