PDA

View Full Version : Submit form using javascript on a link



jc_gmk
04-02-2008, 02:51 PM
I've got a form that is submitted by a link rather than a submit button.

The following code works great with internet explorer but not firefox, any ideas?



<a href="#" onclick="return submititems(this.form);"><img src="content/images/submit.jpg" /></a>

<script type="text/javascript" language="javascript">
function submititems(thisform)
{
for (var q=0, e=this.orderqty.elements, i = e.length-1; i > -1; --i)
{
if (e[i].name && e[i].name=='qty[]' && !isNaN(+e[i].value))
{
q+= +e[i].value;
}
}
if (q==0)
{
alert("you have not selected any products");
}
else
{
if (q==1)
{
if (confirm("add " +q+ " product to the shopping cart?"))
{
document.orderqty.submit();
}
else
{
return false;
}
}
else
{
if (confirm("add " +q+ " products to the shopping cart?"))
{
document.orderqty.submit();
}
else
{
return false;
}
}
}
}
</script>

thetestingsite
04-02-2008, 02:56 PM
onclick="return submititems(this.form);">


is not valid. It should be something like document.forms[0] or document.getElementByIt('formID') or something to that effect.

Hope this helps.

jc_gmk
04-02-2008, 03:24 PM
how exactly do I change my code?
Do i literally change this.form for what you've suggested?

Sorry i'm fairly new to javascript.

thetestingsite
04-02-2008, 04:06 PM
Sorry, I should have been more specific in my last post. Yes, you change the part that I highlighted (this.form) to point to the actual form such as what I posted above.

Hope this helps.

jc_gmk
04-03-2008, 08:58 AM
I've now changed it to:

<a href="#" onclick="return submititems(document.getElementById('product_add'));">
and changed the id of the form to 'product_add'

Which again works fine with Internet Explorer but not firefox.

Also I presume you meant - document.getElementById?

Any other ideas?