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 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    30
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Exclamation Forms with increment function

    Hello to codingforums,
    This is my beginning posting here as I am mostly reading, compiling and so..
    There are multiple forms in the script generated page, that has to update via script url - and the problem is probably looping through form script
    -so please help with this as only first form submit correctly while other pass value from 1st - need looping method or condition?
    Code:
    <SCRIPT language=Javascript>
    function getMessage(hwm)
    {	loc = './index.php?p=ordersBasket&sOption=add';
    	jlc = document.getElementById('qty');
    	jloc = document.getElementById('pid');
    	nloc = loc+ '&iProduct='+jloc.value+ '&iQuantity='+jlc.value;
    	self.location = nloc;
    }
    </SCRIPT>
    <FORM id="123" name="123">
    <input type="button" id="minus" value="-" onClick="qty.value = (qty.value-1)">
    <input type="button" value="+" onClick="qty.value = (+qty.value+1)">
    <input type="text" size="4" id="qty" name="name" value="3" />
    <INPUT TYPE="text" id="pid" VALUE="123" />
    <INPUT TYPE="button" value="order" onclick="getMessage(this)">
    </FORM>
    <FORM id="456" name="456">
    <input type="button" id="minus" value="-" onClick="qty.value = (qty.value-1)">
    <input type="button" value="+" onClick="qty.value = (+qty.value+1)">
    <input type="text" size="4" id="qty" name="name" value="3" />
    <INPUT TYPE="text" id="pid" VALUE="456" />
    <INPUT TYPE="button" value="order" onclick="getMessage(this)">
    </FORM>
    <FORM id="789" name="789">
    <input type="button" id="minus" value="-" onClick="qty.value = (qty.value-1)">
    <input type="button" value="+" onClick="qty.value = (+qty.value+1)">
    <input type="text" size="4" id="qty" name="name" value="3" />
    <INPUT TYPE="text" id="pid" VALUE="789" />
    <INPUT TYPE="button" value="order" onclick="getMessage(this)">
    </FORM>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,441
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Well, it's really a kind of bad idea to have more than one <form> on a page.

    But I don't see why you need *ANY* javascript here at all, if you created the <form>s correctly.

    Code:
    <form action="./index.php" method="get">
    <input type="hidden" name="p" value="OrdersBasket"/>
    <input type="hidden" name="sOption" value="add" />
    <input type="button" value="-" onClick="this.form.iQuantity.value = Number(this.form.iQuantity.value)-1;" />>
    <input type="button" value="+" onClick="this.form.iQuantity.value = Number(this.form.iQuantity.value)+1;" />>
    <input type="text" name="iQuantity" size="4" value="3" />
    <input type="text" name="iProduct" readonly="readonly" value="123" />
    <input type="submit" value="order"/>
    </form>
    <form action="./index.php" method="get">
    <input type="hidden" name="p" value="OrdersBasket"/>
    <input type="hidden" name="sOption" value="add" />
    <input type="button" value="-" onClick="this.form.iQuantity.value = Number(this.form.iQuantity.value)-1;" />>
    <input type="button" value="+" onClick="this.form.iQuantity.value = Number(this.form.iQuantity.value)+1;" />>
    <input type="text" name="iQuantity" size="4" value="3" />
    <input type="text" name="iProduct" readonly="readonly" value="456" />
    <input type="submit" value="order"/>
    </form>
    <form action="./index.php" method="get">
    <input type="hidden" name="p" value="OrdersBasket"/>
    <input type="hidden" name="sOption" value="add" />
    <input type="button" value="-" onClick="this.form.iQuantity.value = Number(this.form.iQuantity.value)-1;" />>
    <input type="button" value="+" onClick="this.form.iQuantity.value = Number(this.form.iQuantity.value)+1;" />>
    <input type="text" name="iQuantity" size="4" value="3" />
    <input type="text" name="iProduct" readonly="readonly" value="789" />
    <input type="submit" value="order"/>
    </form>
    Presto. Only JavaScript used is for the +/- buttons and, if JS is disabled, the user would still be able to type in the quantity by hand.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    n381 (08-30-2011)


  •  

    Tags for this Thread

    Posting Permissions

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