开发者

Submit via javascript doesn't send the textbox value

开发者 https://www.devze.com 2023-03-23 02:15 出处:网络
Interesting problem.I have a client who wants the add to cart button to be an image.That\'s fine, submit via javascript in an onclick on the image.But when I look at the values being sent, quantity is

Interesting problem. I have a client who wants the add to cart button to be an image. That's fine, submit via javascript in an onclick on the image. But when I look at the values being sent, quantity is always 1 (or whatever I set it as in the code). In other words, I can't change the value of quantity.

<form name="AddToCartForm<?=$index?>" id="AddToCartForm<?=$index?>" action="[cartaddress]" method="post">
    <input type="hidden" value="1" name="insert" />
    <input type="hidden" value="<?=$subcategory['Name'];?>" name="category" />
    <input type="hidden" value="<?=$item['ProductNumber'];?>" name="prodnumber" />
    <input type="hidden" value="<?=$item['Name'];?>" name="prodname" />
    <input type="hidden" value="<?=$item['Price'];?>" name="prodprice" />
    <input type="hidden" value=".10" name="handling"/>
    <input type="hidden" value="10" name="weight" />
    <p><?=$item['Description'];?></p>
    <p><strong>Quantity:</strong>&nbsp;<input type="text" name="quantity" size="10" value="1"/></p>
    <p><strong>Price:</strong>&nbsp;$<?=number_format($item['Price'], 2);?></p>
    <p>
        <img onclick="document.getElementById('AddToCartForm'+<?=$index?>).submit();" style="cursor:pointer;" src="images/cart_button.png" width="100" height="111" alt="add to cart" />
    </p>
</form>
开发者_JAVA百科

(If I use a submit button, the quantity goes through.)


You can use <input type="image"> instead of <img>. It acts like submit button.


Your onlcick merely submits, it doesn't do anything to change the value of quantity...so it's doing what you told it to. Before submitting you need to read the value of quantity, add 1 to that, and write the new value back to quantity.


Although the solution is already given, I think I got the cause of the problem:

 document.getElementById('AddToCartForm'+<?=$index?>).submit();

I think the 'onclick' submits the wrong form. Would it also work if you changed it to document.getElementById('AddToCartForm<?=$index?>').submit();?

0

精彩评论

暂无评论...
验证码 换一张
取 消