Page 1 of 1

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

Posted: Wed Oct 15, 2014 5:21 am
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?

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

Posted: Wed Oct 15, 2014 6:41 am
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?

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

Posted: Wed Oct 15, 2014 7:06 am
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'\" />