Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    103
    Thanks
    13
    Thanked 0 Times in 0 Posts

    php var to ajax script via onclick

    Hello Guys,

    Is there a way that I can pass a PHP var to the ajax script via an onclick event? I am trying to pass the var - $imgid

    Here is my link

    PHP Code:
      echo '<a href="artists/'.$artist.'/'.$imgid.'.jpg?imgid='.$imgid.'" onclick="loadXMLDoc()" class="thumbnail" ><img src="artists/'.$artist.'/previews/'.$imgid.'.jpg" border="0" /></a>'
    Here is the Ajax call

    PHP Code:
    <script type="text/javascript">
    function loadXMLDoc()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","artist_title.php?imgid=<?php echo $imgid?>",true);
    xmlhttp.send();
    }
    </script>

    Please advise..
    Thanks, Dan

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Posts
    103
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Every time I add something to the onclick it breaks the ajax call

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    Move the onclick to the img tag. And do you want to load another page, the
    Code:
    <a href="
    in the anchor tag, if your calling an ajax function?


  •  

    Posting Permissions

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