Ajax Help needed: can it run via <a> tag, not Form Button?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Ajax Help needed: can it run via <a> tag, not Form Button?

Post by simonmlewis »

Code: Select all

<script> 
function precheck(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
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("srcHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","/ajax_addtocart.php?q="+str,true);
xmlhttp.send();
}
</script>

<form>
<input type='hidden' name='prodid' value='$row->id'>
<input type='button' value='Request Sample' onclick=\"precheck(prodid.value); document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'\" class='btn_samplecart'>
</form>
This is what I am using at the moment, and it works. So I don't want to change the "working" method of the script.

What I do want to do though, is change it so it runs from an <a> tag, rather than a form button. This is so I can wrap the onclick around an image and information about the product.

I did think of a Javascript in the <a> tag, but that works via a submit, which this isn't.

So how can I wrap this <form> around images, so you "onclick" and it forms the same thing?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Ajax Help needed: can it run via <a> tag, not Form Butto

Post by Celauran »

simonmlewis wrote:I did think of a Javascript in the <a> tag, but that works via a submit, which this isn't.
What?

What's the problem with using an anchor?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Ajax Help needed: can it run via <a> tag, not Form Butto

Post by simonmlewis »

As it turned out, this works too, though I cannot do it to a text link but can to the image:

Code: Select all

<img src='/images/productphotos/small/$row->photoprimary' alt='$row->title' class='sample_imagelink' onclick=\"precheck(form$row->id.prodid.value); document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'\" />
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply