SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get id of the Image and pass to PHP function

    I'm trying to pass the ID of the Image a user clicks on and pass it to a PHP function.

    Please help!

    Here's what I have so far:

    Code:
    <script type="text/javascript">
        /*<![CDATA[*/
        function confirm_results(theform) {
          var text = '\n';
          var inputs = document.getElementsByTagName('img');
          for(var i = 0, n = inputs.length; i < n; i++)
          {
            //text += inputs[i].id + ' ';
            text += inputs[i].id + ' ';
          }
          alert('Are these details correct? ' + text);
        }
        /*]]>*/
        </script>
    The problem with this is, it gets every 'img' and I need just the one that the user clicked on. I also need it to pass the id to a function.

    Thanks!

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you explain this in a bit more detail. What is the purpose of the page?
    Mashups, DOM Scripting and all the things we call Web2.0 blogger
    http://www.heavyweightgeek.org/
    http://twitter.com/heavyweightgeek

  3. #3
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok...here's my code that I'm trying to get to work:
    Code:
    <script type="text/javascript">
        /*<![CDATA[*/
        function confirm_results(id) {
    	var test5= new String(id);
    	  alert(String(test5));
        }
        /*]]>*/
    </script>
    In a PHP function I'm calling this:
    PHP Code:
    echo "<a href=\"#\" onClick=\"javascript:confirm_results(" $id ")\"><img src=" '"' $img '"' " height=\"130\" width=\"130\"" "id=" '"' $id '"' " border=0/></a>"
    Which is outputting this:
    Code:
    <a href="#" onClick="javascript:confirm_results(B000002H97)">
    <img src="http://ecx.images-amazon.com/images/I/212JPT7CWYL.jpg" height="130" width="130"id="B000002H97" border=0/></a>

  4. #4
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It just keeps returning [object]

    I'm doing this cause I want to have the javascript function call a php function but with an id parameter.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll presume your going to fix the other stuff later.

    Pass the id as a string. ie with quotes
    Code:
    onClick="javascript:confirm_results("B000002H97")
    then forget the cdata stuff and just use the following.
    Code:
    function confirm_results(id) {
    	alert(id);
        }
    Mashups, DOM Scripting and all the things we call Web2.0 blogger
    http://www.heavyweightgeek.org/
    http://twitter.com/heavyweightgeek

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry that should be single quotes and without the pseudo javascript protocol too
    Code:
    onclick="confirm_results('B000002H97')"
    Mashups, DOM Scripting and all the things we call Web2.0 blogger
    http://www.heavyweightgeek.org/
    http://twitter.com/heavyweightgeek


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
  •