SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict suzkaw's Avatar
    Join Date
    Apr 2002
    Location
    North Carolina
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Submit Multiple Forms

    Hi I am trying to submit multiple form using javascript and I am not sure if it is even possible.
    Here is an example of what I am trying:
    Code:
    <form method='POST' name='tForm0' action='cart.php'><input type="checkbox" name="optn0" value="1"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Blue<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm1' action='cart.php'><input type="checkbox" name="optn1" value="2"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Red<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm2' action='cart.php'><input type="checkbox" name="optn2" value="3"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Green<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm3' action='cart.php'><input type="checkbox" name="optn3" value="4"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Yellow<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm4' action='cart.php'><input type="checkbox" name="optn4" value="42"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Purple<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm5' action='cart.php'><input type="checkbox" name="optn5" value="43"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Lime<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm6' action='cart.php'><input type="checkbox" name="optn6" value="44"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Lylac<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm7' action='cart.php'><input type="checkbox" name="optn7" value="45"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Maroon<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm8' action='cart.php'><input type="checkbox" name="optn8" value="46"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Teal<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm9' action='cart.php'><input type="checkbox" name="optn9" value="47"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Admiral Blue<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm10' action='cart.php'><input type="checkbox" name="optn10" value="48"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Antique Gold Deep<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm11' action='cart.php'><input type="checkbox" name="optn11" value="49"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Antique White<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <form method='POST' name='tForm12' action='cart.php'><input type="checkbox" name="optn12" value="50"><input type='text' name='quant' size='3' value=''><input type='hidden' name='id' value='paint'><input type='hidden' name='mode' value='add'>Base Flesh<br /><!--<input type="image" src="images/buy.gif" border="0">--></form>
    <input type="submit" name="submitted" value="Submit form" onclick="return formHandler()"></td></tr></table><script language="javascript">
    <!-- //
    function formHandler(){
    document.tform0.submit();
    document.tform1.submit();
    document.tform2.submit();
    document.tform3.submit();
    document.tform4.submit();
    document.tform5.submit();
    document.tform6.submit();
    document.tform7.submit();
    document.tform8.submit();
    document.tform9.submit();
    document.tform10.submit();
    document.tform11.submit();
    document.tform12.submit();
    }
    //-->
    </script>


    and I keep getting this error:
    document.tform0 is null or not an object.
    Thanks Eric.

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Maybe I'm just misinformed, but for the life of me, I can't see why you would want to do it this way(?)

    Generally, forms follow a pattern like

    <form>
    inputs and labels and stuff
    submit button
    </form>

    Whatever you submit to is responsible for sorting out what was entered/selected, other than some basic validation that you do with js before submitting.

    Someone correct me if I'm wrong, but if formHandler() succeeded, wouldn't cart.php be invoked 12 times? (or just the first time with form0?)

    Anyway, case matters - tform0 != tForm0

  3. #3
    SitePoint Addict suzkaw's Avatar
    Join Date
    Apr 2002
    Location
    North Carolina
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea I know but this one is getting posted to a shopping cart and I need a way to take the form from this with a select box:
    Code:
    <form method="POST" name="tForm0" action="cart.php" onSubmit="return formvalidator0(this)">
    	 <tr>
    	   <td colspan="2">
    		 <p>this is a test</p>
    <table border='0' cellspacing='1' cellpadding='1' width='100%'><tr><td align='right' width='30%'><b>Color:</b></td><td> <select onChange='updateprice0();' name='optn0' size='1'><option value=''>Please Select...</option><option value='1'>Blue</option>
    <option value='2'>Red</option>
    <option value='3'>Green</option>
    <option value='4'>Yellow</option>
    <option value='42'>Purple</option>
    <option value='43'>Lime</option>
    <option value='44'>Lylac</option>
    <option value='45'>Maroon</option>
    <option value='46'>Teal</option>
    <option value='47'>Admiral Blue</option>
    <option value='48'>Antique Gold Deep</option>
    <option value='49'>Antique White</option>
    <option value='50'>Base Flesh</option>
    </select></td></tr></table>				</td>
    	 </tr>
    	 <tr>
    	<td width="59%" align="center" valign="middle">
       <b>Price:</b> <span id="pricediv0" name="pricediv0">2.99</span> <br>				</td>
    	   <td align="right" valign="bottom">
    <input type="hidden" name="id" value="paint">
    <input type="hidden" name="mode" value="add">
    <input type="text" name="quant" value="">
    <input type="hidden" name="frompage" value="/mywebs/business/shopping-cart/products.php?id=8">
    <input align="absmiddle" type="image" src="images/buy.gif" border="0"></td>
    	 </tr>
       </form>
    To a multiple form like this:
    http://www.dickblick.com/zz052/92/pr...m=0&ig_id=4833
    with the quantity boxes out to the side.
    Thanks Eric.

  4. #4
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    The html of your example URL (dickblick) is hard to follow, because there's so much of it, but you'll find that there's only one form on the page. (okay, there's 2, one for account stuff, one for the cart stuff)
    Code:
    <html>
    <table>
      <form>
      <tr><td>
        <input type='checkbox' name='cb1'/><input type='text' name='txt1' size='3'/>foo
      </td></tr>
      <tr><td>
      <input type='checkbox' name='cb2'/><input type='text' name='txt2' size='3'/>bar
      </td></tr>
      <tr><td>
        <input type='submit' value='buy stuff' />
      </td></tr>
      </form>
    </table>
    </html>
    If you use something like the above, then on your php, you can check each checkbox to see if it's present and checked, if so, get the quantity from it's associated textbox and add to the cart.

    the point of a form is to invoke some program that will (usually) inspect the request or session to see what the user did/wants. Given that, you only need to get the program rolling by submitting the form. Once that's done, take over from the program you've invoked.

    Its like this post, I'm not sending each word individually, I'm sending a bunch to you and letting you sort it out


Bookmarks

Posting Permissions

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