SitePoint Sponsor

User Tag List

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

    how do i get this variable in to my function???

    HI,

    I have this line in my html:

    Code JavaScript:
    <input type="button" value="edit" value="<?php echo $message_id; ?>" onClick="get_message();" />

    Now the value of the input above is being generated by a php script and is as always a number....theres about 10 of these spit out by the server so it ends up looking like this:

    Code JavaScript:
    <input type="button" id="message_id"value="edit" value="1" onClick="get_message();" />
    <input type="button" id="message_id" value="edit" value="2" onClick="get_message();" />
    <input type="button" id="message_id" value="edit" value="3" onClick="get_message();" />
    and so on.
    Now i have this function ( its actually an ajax function but i only need to ask about the first line of it which is javascript!!) that is currently being run when the button is clicked:

    Code JavaScript:
    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);
     
       }
    Now what i want to happen is for the id that is being generated in the value of the input to make it in to the get_message() function. At present whatever button you click on only the number 1 ever makes it to the script.

    I need a way to get the value of whatever button has been clicked in to my message_id variable.

    Any ideas?


  2. #2
    SitePoint Zealot dustbuster's Avatar
    Join Date
    Jan 2003
    Location
    End-World
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An easy way to do it would be something like this:

    Code JavaScript:
    <input type="button" value="edit" value="<?php echo $message_id; ?>" onClick="get_message(<?php echo $message_id; ?>);" />

    which would give you this

    Code JavaScript:
    <input type="button" id="message_id"value="edit" value="1" onClick="get_message(1);" />
    <input type="button" id="message_id" value="edit" value="2" onClick="get_message(2);" />
    <input type="button" id="message_id" value="edit" value="3" onClick="get_message(3);" />

    Code JavaScript:
    function get_message(num) {
        var message_id = num;
        createRequest();
        var url = "edit_message.php?message_id=" + escape(message_id);
        request.open("GET", url, true);
        request.onreadystatechange = open_message;
        request.send(null);
     
       }

    But the problem you are currently having is due to the fact that all of your ids are the same. In order to get different values you need to have different ids.

    Like so (don't mind the imperfect coding here )
    Code JavaScript:
    <input type="button" id="message_id_<?php echo $message_id; ?>" value="<?php echo $message_id; ?>" onClick="get_message(this);" />

    which would give you this

    Code JavaScript:
    <input type="button" id="message_id_1" value="1" onClick="get_message(this);" />
    <input type="button" id="message_id_2" value="2" onClick="get_message(this);" />
    <input type="button" id="message_id_3" value="3" onClick="get_message(this);" />

    Code JavaScript:
    function get_message(msg) {
        var message_id = msg.value;
        createRequest();
        var url = "edit_message.php?message_id=" + escape(message_id);
        request.open("GET", url, true);
        request.onreadystatechange = open_message;
        request.send(null);
     
       }

    hth a bit
    "If all else fails brute force is always an option."
    M.E.

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi there...thats great...worked a treat and ive also added the 'this' concept to my javascript arsenal also!

    Thanks for your time

  4. #4
    SitePoint Zealot dustbuster's Avatar
    Join Date
    Jan 2003
    Location
    End-World
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    np
    "If all else fails brute force is always an option."
    M.E.


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
  •